Using link_to_remote on RESTful route.

Hiya,

I have the following models:

user user_bookmarks bookmarks

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)

Cheers, Jodi General Partner The nNovation Group inc. www.nnovation.ca/blog

on-innovation.gif

Jodi,

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?

Jodi

jodi:

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

Thanks.

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!

Jodi

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

Jodi

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.

Jodi

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:

http://caboo.se/doc/classes/ActionController/Resources.html

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

hth, jacqui

Jacqui,

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    end

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:

   users_url

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

Jodi