Forum: Ruby on Rails Using link_to_remote on RESTful route.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
chris loper (Guest)
on 2007-02-22 19:39
(Received via mailing list)
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:

------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym

Extracted source (around line #12):

9: <% if logged_in? %>
10:
11: <p class="user_bookmark_<%= @item.id %>">
12:   <%= link_to_remote 'Bookmark!', :url => user_bookmarks_path
(current_user, @item) %>
13: </p>
14:
15: <% end %>

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/core_ext/hash/keys.rb:27:in `symbolize_keys'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/
active_record/base.rb:1941:in `inject'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/core_ext/hash/keys.rb:26:in `each'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/core_ext/hash/keys.rb:26:in `inject'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/core_ext/hash/keys.rb:26:in `symbolize_keys'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/
helpers/url_helper.rb:21:in `url_for'
(eval):19:in `user_bookmarks_path'
#{RAILS_ROOT}/app/views/items/show.rhtml:12:in
`_run_rhtml_47app47views47items47show46rhtml'
app/controllers/items_controller.rb:20:in `show'
app/controllers/items_controller.rb:20:in `show'
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------

If anyone could help out, i'd appreciate it.
Jodi S. (Guest)
on 2007-02-22 19:44
(Received via mailing list)
On 22-Feb-07, at 12:35 PM, chris loper wrote:

> 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) %>
>
>

> could help out, i'd appreciate it.

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
chris loper (Guest)
on 2007-02-22 20:00
(Received via mailing list)
On Feb 22, 2007, at 9:43 AM, Jodi S. wrote:
>
> 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)

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"}
Jodi S. (Guest)
on 2007-02-22 20:18
(Received via mailing list)
On 22-Feb-07, at 12:59 PM, chris loper wrote:

> Jodi,
>
> I tried this but got the following error:
>
> user_bookmarks_url failed to generate from
> {:item=>"1", :action=>"index", :user=>"1", :controller=>"user_bookmark
> s"
> }, expected: {:action=>"index", :controller=>"user_bookmarks"}, diff:
> {:item=>"1", :user=>"1"}

Chris, can you please post the applicable route?

Jodi
chris loper (Guest)
on 2007-02-23 04:34
(Received via mailing list)
jodi:

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

Thanks.
Jodi S. (Guest)
on 2007-02-23 06:49
(Received via mailing list)
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
Alan F. (Guest)
on 2007-02-23 16:23
Jodi S. wrote:
> 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

Hmm, surely the plural path shouldn't need a bookmark ID ?
Jodi S. (Guest)
on 2007-02-23 16:40
(Received via mailing list)
On 23-Feb-07, at 9:23 AM, Alan F. wrote:

>
> Hmm, surely the plural path shouldn't need a bookmark ID ?

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

Jodi
Jodi S. (Guest)
on 2007-02-23 16:47
(Received via mailing list)
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
jacqui (Guest)
on 2007-02-23 18:16
(Received via mailing list)
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
Jodi S. (Guest)
on 2007-02-23 18:32
(Received via mailing list)
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
This topic is locked and can not be replied to.