Topic: map.resource, :has_one, route generation error

Hello.

I'm having a similar problem to this one: http://railsforum.com/viewtopic.php?id=28724

I have a user model (has_one :evenplaner) and a evenplaner model (belongs_to :user)

routes.rb:

map.resources :user, :has_one => :evenplaner

first, rake routes gives me:

new_user_evenplaner GET    /users/:user_id/evenplaner/new(.:format) {:action=>"new", :controller=>"evenplaners"}
edit_user_evenplaner GET    /users/:user_id/evenplaner/edit(.:format) {:action=>"edit", :controller=>"evenplaners"}
user_evenplaner GET    /users/:user_id/evenplaner(.:format) {:action=>"show", :controller=>"evenplaners"}
etc...

AS you can see, at the end of each route I get the .:format part, but I'm missing the :id part

Second, when I use form_for([@user, @evenplaner]) I get a no method error user_evenplaners_path (I get an S thats not supposed to go there)

I followed the post cited above solution for the form, and it worked, but the other restful routes aren't working since they're missing the :id part.

Any advice? Thanks.

Last edited by guapolo (2010-05-03 02:45:02)

Re: map.resource, :has_one, route generation error

guapolo wrote:

AS you can see, at the end of each route I get the .:format part, but I'm missing the :id part

Hello,

I suppose you don't need the :id part as Rails can automatically find the eventplanner associated to your user (if it exists) as it is a has_one relationships. Why do you need the :id part?

Re: map.resource, :has_one, route generation error

Thanks for your reply.

The thing is that being a RESTFUL route, you need de :id part for some actions, like show: I get routes like

http://localhost/evenplan/users/11/evenplaner.%23%3Cevenplaner:0x1039f02c8%3E

which end taking me nowhere...

Re: map.resource, :has_one, route generation error

I think that if you define your route with "map.resources :user, :has_one => :evenplaner" you can only create routes for an evenplaner in the context of its user, meaning that you can not create a route for a evenplaner with its id. For that, you would have to define a dedicated for the resource evenplaner: "map.resources :evenplaner".

Having only "map.resources :user, :has_one => :evenplaner", if you want to show a specific evenplaner @evenplaner, you have to go through its associated user:

user_evenplaner_path(@evenplaner.user)

Hope it makes sense smile

Re: map.resource, :has_one, route generation error

Yep, it does make sense.

I'm going  through the user model. The buggy part is that using

user_evenplaner_path(@evenplaner.user)

generates a link with the route

http://localhost/evenplan/users/11/evenplaner.%23%3Cevenplaner:0x1039f02c8%3E

if I use the following form_for syntax,

form_for([@user, @evenplaner])

I get the error "user_evenplaners_path" does not exist. And it don't, since map.resources users, :has_one => :evenplaner should generate "user_evenplaner_path" method, without the "s" on evenplaner.

Hope it makes sense wink

Re: map.resource, :has_one, route generation error

Found the solution in here: http://geekninja.blogspot.com/2008/08/m … prise.html

The thing is that when using resources with the :has_one option, you can't use arrays for generating urls
(here I can't use [@user, @evenplaner], have to use user_evenplaner_path(@user)). I was using in the controllers respond_to part redirect_to [@user, @evenplaner], hence the route was wrong. Changed it to use user_evenplaner_path(@user) and worked like a charm.

Thanks for the help!

Last edited by guapolo (2010-05-03 12:50:00)

Re: map.resource, :has_one, route generation error

Good to see you found a solution smile