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:
12: <%= link_to_remote ‘Bookmark!’, :url => user_bookmarks_path
(current_user, @item) %>
13:
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.
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
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:
map.resources :users do |user|
user.resources :user_bookmarks
end
Thanks.
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
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
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 ?
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
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