Ajax problem - works once, but not again

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

Not sure if this is the cause of the problem but if the div "

" is inside the partial items_ratings and it is the target of
link_to_remote update when you click the link the page is going to
look like
... rating here ...
.. try to find an existing rating ...

You may have taken out too much information, whats the div
link_to_remote is updating?

  • Richard

Richard L. wrote:

Not sure if this is the cause of the problem but if the div "

" is inside the partial items_ratings and it is the target of
link_to_remote update when you click the link the page is going to
look like
... rating here ...
.. try to find an existing rating ...

You may have taken out too much information, whats the div
link_to_remote is updating?

  • Richard

Ah, that looks to be the case! I see that the div is indeed repeated
twice. Here’s a code that names the div (on the main page there’s a
listing of items, that’s why I have to dynamically construct the div
name):

_item.rhtml that contains _items_ratings.rhtml:

<% @rating_box_id = ‘rating_box_’ + item.id.to_s %>

<%= render(:partial => ‘items_ratings’, :locals => { :item => item,
:id => item.id }) -%>

Inside _items_ratings.rhtml:

">
...

<%= link_to_remote (image_tag("/images/arrow-up-d.gif"), :update=> 

@rating_box_id, :url =>{ :controller => :items, :action =>
:remove_rating, :id => item.id})%>

...

I think I understand what the problem is… but how do I fix it?

Any ideas, anyone?