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

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

items.rhtml:
...
<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 => item.id})%>
	</td>
   </tr>
   <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 }) %>
     </td>
   </tr>
...


_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!
8493b7fca6ae697dfc7fe7d1aa4a3125?d=identicon&s=25 Jake J. (janovetz)
on 2006-05-02 06: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_<%= item.id %>" ...

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

   Jake
This topic is locked and can not be replied to.