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! -- Posted via http://www.ruby-forum.com/.
Jake Janovetz
2006-May-02 04:11 UTC
[Rails] Re: Newbie problem with Ajax and render partial
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 -- Posted via http://www.ruby-forum.com/.