Hi all,
I have a problem with render partial and all that good stuff. I have a
link that updates a number (increment rating) when clicked in an
Ajaxy-way, without need to refresh. Clicking it for the first time works
perfectly, but after it updates the number, if I click it again (it’s
supposed to remove rating) - it doesn’t work. Only if I refresh the
screen, it works properly (removes the rating).
Here’s the code (I tried to remove as much irrelevant stuff as
possible):
_items_ratings.rhtml:
... rating here ...
.. try to find an existing rating ...
<% if @rating && @rating.value == 1 %>
<%= link_to_remote (image_tag("/images/arrow-up-d.gif"), :update
=> this div, :url =>{ :controller => :items, :action => :remove_rating,
:id => item.id})%>
<% else %>
<%= link_to_remote (image_tag("/images/arrow-up.gif"), :update
=> this div, :url =>{ :controller => :items, :action =>
:increment_rating, :id => item.id})%>
<% end %>
items_controller:
def increment_rating
@item = Item.find(params[:id])
redirect_to :controller => ‘rating’, :action => ‘increase’, :item_id
=> @item.id
end
def remove_rating
@item = Item.find(params[:id])
redirect_to :controller => ‘rating’, :action => ‘remove’, :item_id
=> @item.id
end
rating_controller:
def increase
.. increase rating in the db ...
render(:partial => 'items/items_ratings', :locals => { :item =>
@item, :id => @item.id })
end
def remove
.. remove rating from the db ...
render(:partial => 'items/items_ratings', :locals => { :item =>
@item, :id => @item.id })
end