named routes / has_one problem
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Wolfgang Klinger <wolfgang.klin... @gmail.com>
Date: Tue, 4 Dec 2007 21:54:18 -0800 (PST)
Local: Wed, Dec 5 2007 4:54 pm
Subject: named routes / has_one problem
*hiya!*
I have a problem with named routes and has_one
(Rails 1.99.1)
My example:
class Car < ActiveRecord::Base
has_one :wheel
end
--- config/routes.rb
map.resources wheels
map.resources cars, :has_one => [ :wheel ]
Now if I try car_wheel_path(@car, @car.wheel) I get
undefined method `has_key?' for #<Wheel:0x48e3e20>
car_wheel_path(@car) works but the url is wrong (at least I think
so): /cars/1/wheel
and l get "Couldn't find Wheel without an ID"
rake routes outputs:
------ POST /cars/:car_id/wheel
{:controller=>"wheels", :action=>"create"}
------ POST /cars/:car_id/wheel.:format
{:controller=>"wheels", :action=>"create"}
new_car_wheel GET /cars/:car_id/wheel/new
{:controller=>"wheels", :action=>"new"}
formatted_new_car_wheel GET /cars/:car_id/wheel/new.
:format {:controller=>"wheels", :action=>"new"}
edit_car_wheel GET /cars/:car_id/wheel/edit
{:controller=>"wheels", :action=>"edit"}
formatted_edit_car_wheel GET /cars/:car_id/wheel/edit.:format
{:controller=>"wheels", :action=>"edit"}
car_wheel GET /cars/:car_id/wheel
{:controller=>"wheels", :action=>"show"}
formatted_car_wheel GET /cars/:car_id/wheel.:format
{:controller=>"wheels", :action=>"show"}
------ PUT /cars/:car_id/wheel
{:controller=>"wheels", :action=>"update"}
------ PUT /cars/:car_id/wheel.:format
{:controller=>"wheels", :action=>"update"}
------ DELETE /cars/:car_id/wheel
{:controller=>"wheels", :action=>"destroy"}
------ DELETE /cars/:car_id/wheel.:format
{:controller=>"wheels", :action=>"destroy"}
Any ideas to solve this? How do I get the wheel_id for the wheels
controller? I'm really stuck :-(
thanks
bye Wolfgang
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
thom <thomas.wei... @gmail.com>
Date: Mon, 10 Dec 2007 11:15:50 -0800 (PST)
Local: Tues, Dec 11 2007 6:15 am
Subject: Re: named routes / has_one problem
Hi
I've got exactly the same problem.
Did you find any solution until now?
Thanks,
Thomas
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"James H." <james.herd... @gmail.com>
Date: Mon, 10 Dec 2007 14:14:40 -0800 (PST)
Local: Tues, Dec 11 2007 9:14 am
Subject: Re: named routes / has_one problem
Thomas,
Looking at Wolfgang's example, I'm wondering if there's an inflection
issue going on. Does Rails expect a pluralized controller name for
singleton resources, or plural?
James
On Dec 10, 2:15 pm, thom <thomas.wei... @gmail.com> wrote:
> Hi
> I've got exactly the same problem.
> Did you find any solution until now?
> Thanks,
> Thomas
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
thom <thomas.wei... @gmail.com>
Date: Tue, 11 Dec 2007 09:08:13 -0800 (PST)
Local: Wed, Dec 12 2007 4:08 am
Subject: Re: named routes / has_one problem
Hi James
In my case I have the following models
# id :integer(11) not null, primary key
class User < ActiveRecord::Base
has_one :profile
end
# id :integer(11) not null, primary key
# user_id :integer(11)
class Profile < ActiveRecord::Base
belongs_to :user
end
config/routes.rb looks like this
map.resources :users do |users|
users.resource :profile, :controller => 'profile'
end
It does not really matter whether I'm using ProfileController or
ProfilesController, I get the same error as soon as I'm using
<% form_for [@user, @profile] do |f| %>
in app/views/profile/edit.html.erb.
But if I use
<% form_for :profile, @profile, :url => user_profile_path(@user),
:html => { :method => :put } do |f| %>
it works without any problems.
Thomas
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"E. James OKelly" <e.james.oke... @gmail.com>
Date: Tue, 11 Dec 2007 09:16:50 -0800
Local: Wed, Dec 12 2007 4:16 am
Subject: Re: named routes / has_one problem
Are you using rails 2.0.1? I have the following in some test code I wrote and it works fine for me.
map.resources :posts, :has_many => :comments
class Comment < ActiveRecord::Base belongs_to :post end
class Post < ActiveRecord::Base has_many :comments end
<% form_for([@post, @comment]) do |f| %>
<% end %>
On Dec 11, 2007 9:08 AM, thom <thomas.wei... @gmail.com> wrote:
> Hi James
> In my case I have the following models
> # id :integer(11) not null, primary key > class User < ActiveRecord::Base > has_one :profile > end
> # id :integer(11) not null, primary key > # user_id :integer(11) > class Profile < ActiveRecord::Base > belongs_to :user > end
> config/routes.rb looks like this
> map.resources :users do |users| > users.resource :profile, :controller => 'profile' > end
> It does not really matter whether I'm using ProfileController or > ProfilesController, I get the same error as soon as I'm using
> <% form_for [@user, @profile] do |f| %>
> in app/views/profile/edit.html.erb.
> But if I use
> <% form_for :profile, @profile, :url => user_profile_path(@user), > :html => { :method => :put } do |f| %>
> it works without any problems.
> Thomas
--
E. James O'Kelly
Ruby on Rails Evangelist
Software Engineer
760 777-6418
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"E. James OKelly" <e.james.oke... @gmail.com>
Date: Tue, 11 Dec 2007 09:18:54 -0800
Local: Wed, Dec 12 2007 4:18 am
Subject: Re: named routes / has_one problem
Without seeing the code it is going to be difficult to see where the problem truly lies. If you would like one of you can send me your codebase and I can get it to work and then post the fix here. The easiest way it to zip it and upload it to mediafire.com and send me the download link to james{at}railsjitsu(dot)com.
I have some time open today I could work on this for you.
On Dec 11, 2007 9:16 AM, E. James OKelly <e.james.oke... @gmail.com> wrote:
> Are you using rails 2.0.1? I have the following in some test code I wrote
> and it works fine for me.
> map.resources :posts, :has_many => :comments
> class Comment < ActiveRecord::Base > belongs_to :post > end
> class Post < ActiveRecord::Base > has_many :comments > end
> <% form_for([@post, @comment]) do |f| %>
> <% end %>
> On Dec 11, 2007 9:08 AM, thom < thomas.wei... @gmail.com> wrote:
> > Hi James
> > In my case I have the following models
> > # id :integer(11) not null, primary key > > class User < ActiveRecord::Base > > has_one :profile > > end
> > # id :integer(11) not null, primary key > > # user_id :integer(11) > > class Profile < ActiveRecord::Base > > belongs_to :user > > end
> > config/routes.rb looks like this
> > map.resources :users do |users| > > users.resource :profile, :controller => 'profile' > > end
> > It does not really matter whether I'm using ProfileController or > > ProfilesController, I get the same error as soon as I'm using
> > <% form_for [@user, @profile] do |f| %>
> > in app/views/profile/edit.html.erb.
> > But if I use
> > <% form_for :profile, @profile, :url => user_profile_path(@user), > > :html => { :method => :put } do |f| %>
> > it works without any problems.
> > Thomas
> -- > E. James O'Kelly > Ruby on Rails Evangelist > Software Engineer > 760 777-6418
--
E. James O'Kelly
Ruby on Rails Evangelist
Software Engineer
760 777-6418
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Thomas Weibel" <thomas.wei... @gmail.com>
Date: Thu, 13 Dec 2007 14:51:05 +0100
Local: Fri, Dec 14 2007 12:51 am
Subject: Re: named routes / has_one problem
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Steven <shi.ste... @gmail.com>
Date: Sun, 30 Dec 2007 02:05:52 -0800 (PST)
Local: Sun, Dec 30 2007 9:05 pm
Subject: Re: named routes / has_one problem
The helper form_for does not work for nested singleton resource in
Rails 2.0
Given below nested singleton resource profile,
map.resources :users do |user|
user.resource :profile
end
<% form_for([@user, @profile]) do |f| %>
actually expects to get the url from helper "user_profileS_url ",
instead of "user_profile_url". The latter url helper is the one
generated by routes.rb.
<% form_for([:user, @profile]) do |f| %> generates url like /user/
profile, which is for named route, not for singleton resource.
Any idea?
Steven
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
paron <rphill... @engineer.co.summit.oh.us>
Date: Wed, 27 Feb 2008 07:54:27 -0800 (PST)
Local: Thurs, Feb 28 2008 2:54 am
Subject: Re: named routes / has_one problem
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
padboo <gle... @gmail.com>
Date: Fri, 7 Mar 2008 00:53:53 -0800 (PST)
Local: Fri, Mar 7 2008 7:53 pm
Subject: Re: named routes / has_one problem
Hello,
I have the same problem as the originator of this thread, and still
cannot get it to work.
I have a simple example:
In my routes.rb I have the following:
map.resources :cars :has_one => [:motor]
I'm trying to get the url for editing the motor object, which should
be http://mysite/cars/1/motor/edit
However, when I call either car_motor_path(@car, @car.motor) or
edit_car_motor_path(@car, @car.motor) I get:
undefined method `has_key?' for #<Motor:0x44a75f0>
Any ideas?
Thanks, Leo.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
AndyV <a... @cornell.edu>
Date: Sat, 8 Mar 2008 10:21:39 -0800 (PST)
Local: Sun, Mar 9 2008 5:21 am
Subject: Re: named routes / has_one problem
Not sure if you have a typo or not, but the url should be:
http://mysite/cars/1/motor/1/edit .
Edit is a member method and you must supply an id for it to work.
Also, I noticed some inconsistencies in the path rendering in late
1.2.x and pre 2.0 releases. In 1.99.x you may need to specify:
edit_car_motor_path(:car_id=>@car.id, :id=>@car.motor.id)
It's probably worth upgrading to 2.x rather than hack around this.
On Mar 7, 3:53 am, padboo <gle... @gmail.com> wrote:
> Hello,
> I have the same problem as the originator of this thread, and still
> cannot get it to work.
> I have a simple example:
> In my routes.rb I have the following:
> map.resources :cars :has_one => [:motor]
> I'm trying to get the url for editing the motor object, which should
> behttp://mysite/cars/1/motor/edit
> However, when I call either car_motor_path(@car, @car.motor) or
> edit_car_motor_path(@car, @car.motor) I get:
> undefined method `has_key?' for #<Motor:0x44a75f0>
> Any ideas?
> Thanks, Leo.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
paron <rphill... @engineer.co.summit.oh.us>
Date: Tue, 11 Mar 2008 04:13:38 -0700 (PDT)
Local: Tues, Mar 11 2008 10:13 pm
Subject: Re: named routes / has_one problem
On Mar 8, 2:21 pm, AndyV <a
... @cornell.edu> wrote:
> Not sure if you have a typo or not, but the url should be:
> http://mysite/cars/1/motor/1/edit .
> Edit is a member method and you must supply an id for it to work.
I don't know about that; if you look at the tut I posted, "<%=link_to
'Show', trip_expenditure_path(@trip)>" gives 'trips/:trip_id/
expenditure' and it works just fine.
I think padboo could call "car_motor_path(@car)"; the :motor_id is
implicit since there's only one.
Ron
You must
Sign in before you can post messages.
You do not have the permission required to post.