Problem with nested resource, object name and routes

0 views
Skip to first unread message

goodieboy

unread,
Apr 17, 2008, 8:05:44 AM4/17/08
to resource_controller
Hi,

I have a User resource and a UserLink resource. I want to be able to
go to:

/users/3/links

to see the links that belong to a particular user. My routes are setup
like:

map.resources :users do |umap|
umap.resources :links, :controller=>'users/links'
end

The problem is when I try to use one of the a_r route/path helpers I
get an error. Here is the error from using the collection_path helper,
inside of the /users/links/index.html.erb view:

undefined method `users_user_users_links_path' for
#<Users::LinksController:0x26b360c>

My url in that case is: /users/3/links. My links controller looks
like:

class Users::LinksController < ApplicationController

resource_controller

belongs_to :user
model_name :user_link
route_name :users_links
object_name :user_link

def parent_association
@parent_object ||= parent_object
parent_object.links
end

end

I've tried every combination of values for the class method calls
above, but still the path helper never seems to resolve correctly.

Any ideas as to what's going on?

Thanks,
Matt

Mark Lynn

unread,
Apr 17, 2008, 9:14:29 AM4/17/08
to resource_...@googlegroups.com

If you want links like

/users/3/links

that should be pretty straightforward. In particular, you don't need
any of the overrides in your controller to achieve that.

There might be a little extra confusion in naming your nested resource
UserLinks. I would just call this Links. The fact that they belong to
users is handled automatically by the nesting. Then, your route would be

map.resources :users, :has_many => :links

Try "rake routes" from the command line now and then try it with the
above route and see the difference in the routes you are generating. I
have several resources with paths like this in my current app and the
controllers are pretty much empty. In particular, you should not need
to override model_name, route_name, and object_name like this. Also,
make sure your associations are setup properly in your user and link
models and you should not need to define parent_association either.

- Mark

Reply all
Reply to author
Forward
0 new messages