Newbie problem with Ajax and render partial

Hi all,

I’m having this problem that I’m sure will take most of you all but two
seconds to solve, but I’m very much a newbie. I read the documentation,
googled for an answer, to no avail.

I have simple page that lists items, each item as a row in a table. Each
item has a rating and a link to increment/decrement rating. When using
+/- on a rating on the first item, it works fine. However, if done on
any other item, it updates it in the database fine, but shows the first
item being updated, until the page refresh. Here’s the code:

items.rhtml:

<% for item in @items %>
...

_rating_box.rhtml:

<%=h item.rating %>

items_controller.rb:

def increment_rating
@item = Item.find(params[:id])
@item.rating = @item.rating + 1
@item.update_attributes(params[:item])
render(:partial => ‘rating_box’, :locals => { :item => @item })
end

Thank you very much for your help!

<%= link_to h (item.description), :action => 'show', :id => item %> <%= link_to_remote ('+', :update => 'rating_box', :url =>{ :action => :increment_rating, :id => item.id})%>
<%=h (truncate(item.text, 80)) %> <%= render (:partial => 'rating_box', :locals => { :item => item }) %>

Alex wrote:

 <td width="2%" align="right" id='rating_box'>
     <%= render (:partial => 'rating_box', :locals => { :item => 

item }) %>

Hi Alex-

First, you should have double quotes around the ID field.

With this, you’re going to end up with a lot of ‘td’ elements with the
ID “rating_box”. You should append the id (or some other unique
identifier) to be able to discern them. Appending the ID is simple and
convenient. You’ll just have:

… id=“rating_box_<%= item.id %>” …

That should help. Unless I don’t understand the problem you’re having.

Jake