Forum: Ruby on Rails Newbie problem with Ajax and render partial

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alex (Guest)
on 2006-05-02 07:35
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:

<table border="1" cellpadding="5" cellspacing="0">
<% for item in @items %>
<tr valign="top">
  <table width="100%" border="1" cellpadding="5" cellspacing="0">
  <tr height="20%">
    <td width="98%"><%= link_to h (item.description), :action => 'show',
:id => item %></td>
	<td width="2%" align="right">
        <%= link_to_remote ('+', :update => 'rating_box', :url =>{
:action => :increment_rating, :id =>})%>
   <tr height="60%">
     <td width="98%"><%=h (truncate(item.text, 80)) %></td>
     <td width="2%" align="right" id='rating_box'>
         <%= render (:partial => 'rating_box', :locals => { :item =>
item }) %>


<%=h item.rating %>

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

Thank you very much for your help!
Jake J. (Guest)
on 2006-05-02 08:10
Alex wrote:
>      <td width="2%" align="right" id='rating_box'>
>          <%= render (:partial => 'rating_box', :locals => { :item =>
> item }) %>
>      </td>

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_<%= %>" ...

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

This topic is locked and can not be replied to.