Ajax update one element of iterated variable - Rails 3
Here's my view:
<% provide(:title, 'Stores') %>
<table class="table table-condensed table-hover">
<tr>
<th>Name</th>
<th>Address</th>
<th>Favorite?</th>
</tr>
<% @vendors.each do |v| %>
<tr>
<td><%= v.name %></td>
<td><%= v.address %></td>
<td id="toggle">
<% if current_user.voted_for?(v) %>
<%= link_to image_tag("Heart (1).png"), { :controller =>
:vendors, :action => 'vote_against_vendor', :vendor_id =>
v.id},
{ :method => 'delete', :remote =>
true }%>
<% else %>
<%= link_to image_tag("Heart (2).png"), { :controller =>
:vendors, :action => 'vote_for_vendor', :vendor_id =>
v.id},
{ :method => 'post', :remote => true} %>
<% end %>
</td>
</tr>
<% end %>
</table>
These remote links are going to these methods in my controller, and
they're working okay:
class VendorsController < ApplicationController
def vote_for_vendor
begin
vendor = Vendor.find(params[:vendor_id])
current_user.vote_for(vendor)
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
def vote_against_vendor
begin
vendor = Vendor.find(params[:vendor_id])
current_user.unvote_for(vendor)
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
I'm using the thumbs_up gem for my vote methods.
I'm now working on trying to get an ajax update working. I want to update
only the respective #toggle td for the link that was clicked. I'm fairly
new to using ajax and js, so I'm really not sure how to accomplish this. I
mostly understand how to update a div while not iterating, but I'm not
sure how my situation will work. Help? Thanks in advance!
No comments:
Post a Comment