I am implementing a "favorites" feature into a community blog application, and would like to use Ajax.
When users like a front page post, there is a "+" symbol they can click to add that post to their collection of favorites.
The feature works great without Ajax functionality. But now I'm adding the Ajax.
In the view:
<span id="favorite_<%= entry.id %>">
<%= link_to_remote "+", :url => { :action => 'add_favorite' },
:entry => entry.id,
:user => session[:user_id],
:update => 'favorite_' + entry.id.to_s %>
</span>
The corresponding controller action:
def add_favorite
@favorite = Favorite.new(:entry_id => :entry, :user_id => :user)
if @favorite.save
render :text => "added to favorites", :layout => false
end
end
The text is rendering appropriately in the view, as though the action has worked. But when I check the list of favorites, it hasn't been added.
I ran the line
@favorite = Favorite.new(:entry_id => :entry, :user_id => :user)
in the interactive shell and it was all good. Everything saved.
What am I overlooking/doing wrong?