RESTful Nesting issue

Iâ??m having a strange issue with nested routes though, and was wondering
if anyone could shed some light on thisâ?¦

If I have:

map.resources :users do |users|
users.resources :items
end

When I try to call

<%= link_to_remote ‘Delete’, :url => item_url(@item), :with =>
“’_method=delete’”, :confirm => ‘Are you sure?’ %>

From inside the user url

http://localhost:3001/users/1/items

Or edit_item_url(@item.id) it doesnâ??t always render the correct URL, and
sometimes I get a nil error trying to render the url.

Not sure what Iâ??m missing here, but any help is greatly appreciated.
Thanks.

I’ve freezed Edge Rails btw.

Still getting to grips with this myself, but I think you still need to
explicitly declare the items routes as well, so:
map.resources :items

Rick’s routing_navigator helps with showing what routes are available
and that’s help me some, although as I said, still getting to grips with
it myself…

HTH

Guest wrote:

Iâ??m having a strange issue with nested routes though, and was wondering
if anyone could shed some light on thisâ?¦

If I have:

map.resources :users do |users|
users.resources :items
end

When I try to call

<%= link_to_remote ‘Delete’, :url => item_url(@item), :with =>
“’_method=delete’”, :confirm => ‘Are you sure?’ %>

From inside the user url

http://localhost:3001/users/1/items

Or edit_item_url(@item.id) it doesnâ??t always render the correct URL, and
sometimes I get a nil error trying to render the url.

When you use the x_url or x_path helpers you can pass either a hash of
options or positional params. If you go with positional params, they
have to correspond to the order of params in the route. In your example,
the route URL would look like

/users/:user_id/items/:id

Therefore you need to say

item_url(@user, @item)
or
items_url(@user)

That should fix it. You might also consider using item_path instead of
item_url. It’s the same without the protocol, host and port bits of the
URL, which are generally redundant for that usage.


Josh S.
http://blog.hasmanythrough.com

try

<%= link_to_remote ‘Delete’, :url => item_url(@item.user, @item), :with
=>
“’_method=delete’”, :confirm => ‘Are you sure?’ %>

(asuming Item has a user association)
nesting broke my neck yesterday, too, because i didn’t think of the
:with
option and tried
<%= link_to_remote ‘delete’, :url => item_url(@item.user, @item,
:_method =>
‘delete’) %>
but, as you can think that doesnt work at all

2006/8/5, Guest [email protected]:

When I try to call

http://lists.rubyonrails.org/mailman/listinfo/rails


Michael S. [email protected]

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

This is all pretty helpful. Thanks guys. Another thing, is there a way
to get the user from the url?

I’m going to complicate this even more now…

Items can belong to users, and categories. So…

I have this in my routes file:

map.resources :categories do |categories|
categories.resources :items
end

map.resources :users do |users|
users.resources :items
end

Now, if I’m at users/1/items I need a way of being able to have that
user id. However if I’m in categories/1/items I need the category ID. Do
I have to do an explicit find for each in the index action for items
like:

@user = User.find(params[:user_id])

and

@categroy = Category.find(prams[:category_id])

?

There has to be a better way than this.

Thanks again.

Michael S. wrote:

try

<%= link_to_remote ‘Delete’, :url => item_url(@item.user, @item), :with
=>
“’_method=delete’”, :confirm => ‘Are you sure?’ %>

(asuming Item has a user association)
nesting broke my neck yesterday, too, because i didn’t think of the
:with
option and tried
<%= link_to_remote ‘delete’, :url => item_url(@item.user, @item,
:_method =>
‘delete’) %>
but, as you can think that doesnt work at all

2006/8/5, Guest [email protected]:

When I try to call

http://lists.rubyonrails.org/mailman/listinfo/rails


Michael S. [email protected]

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

On Aug 6, 2006, at 10:47 AM, Guest wrote:

categories.resources :items

like:

@user = User.find(params[:user_id])

and

@categroy = Category.find(prams[:category_id])

?

There has to be a better way than this.

I think this is pretty nice:

before_filter :find_category
before_filter :find_item, :except => [:index, :new, :create]

private
def find_category
@category = Category.find(params[:category_id])
end

 def find_item
   @item = @category.items.find(params[:id])
 end

Is it too much code? or too repetitive? Could dry it out for
resources which follow the /categories/1/items/2 convention -

in a base class

before_filter :find_resources

imagine resource_chain is extracted from the route as

[[‘categories’, 1], [‘items’, 2]]

set @category = Category.find(1), @item = @category.items.find

(2), etc.
def find_resources
name, id = resource_chain.shift
root = name.classify.constantize.find(id)
instance_variable_set “@#{name.singularize}”, root
resource_chain.inject(root) do |last, (associate, id)|
instance_variable_set “@#{associate.singularize}”, last.send
(associate).find(id)
end
end

Then your resource controllers can drop the find_* before_filters.

jeremy