Using link_to_remote on RESTful route.


I have the following models:


It is a standard "has_many :through" relationship. Here is the issue though, I am using link_to_remote to add the bookmark for that wonderful ajax feeling.

<%= link_to_remote 'Bookmark!', :url => user_bookmarks_path(current_user, @item) %>

I am using TechnoWeenies Restful_Authentication plugin (hence the current_user object), but I can't seem to get this to work. This *should* be POSTing via AJAX to UserBookmarks#create but I can't even get that far.

Here is the error:

Hey Chris;

I've found that keying the params is needed (might just be for nested resources, as I'm still wrestling a bit with the helpers),

try : user_bookmarks_path(:user => current_user, :bookmark => @item)

General Partner
The nNovation Group inc.



I tried this but got the following error:

user_bookmarks_url failed to generate from {:item=>"1", :action=>"index", :user=>"1", :controller=>"user_bookmarks"}, expected: {:action=>"index", :controller=>"user_bookmarks"}, diff: {:item=>"1", :user=>"1"}

Chris, can you please post the applicable route?



map.resources :users do |user|
     user.resources :user_bookmarks


ok chris, this seems to work on my end:

<%= link_to_remote 'Bookmark!', :url => user_bookmarks_path(:user_id
=> current_user, :bookmark_id => @item) %>

the _id's have it!


the resource user_bookmarks is an intersection model - so both sides of the intersection are required.


what I meant to say Alan was that the controller logic requires the
bookmark_id to create the join.

It's not needed (as you were likely saying) to generate the url.


I thought that the plural version of the helper did NOT require an id
and the singular version did. The plural version of the restful
routing helper methods are for linking to collections of objects. The
singular version is for linking to a specific object and thus requires
the id for it.

More information on routes and resources is available here:

Note especially the use of the singular and plural routing helpers and
their respective required params:
    # GET messages_url
    # GET message_url(:id => 1)



first of all I'm still getting this stuff straight, but this is how I
understand this.

First this is a nested resource, so the parent id is required. And
secondly this is a create, so we're dealing with a POST.

given the route definition:

   map.resources :users do |user|
        user.resources :user_bookmarks

to create a user_bookmark (the object of this exercise) we use the
plural helper to add to the collection.

so user_bookmarks_url

but since user_bookmarks is nested, we must supply the parent
resource id (user).

   user_bookmarks_url(:user_id => current_user)

if we were just creating a user(a non-nested resource) then the
helper call would look like:


I'm I daffy? btw, the above works well in a test app modeled on
Chris' supplied routes and model definitions.