I am new to rails so go easy on me. I have built a blog and I am attempting to add a "vote up" type feature to the /posts/index.html.erb page. Kind of like Facebooks's like feature. I have can successfully get the vote form to work. I have successfully been able to get the posts rendererd out through the <% @posts.each do |post| %> call. Here is where I have the problem. I then tell index, in the above, to also render for each post the vote form and also the <%= post.votes.count %> to display the total votes for each post and allow for submitting the form for each post. I am using AJAX to allow this all to work together.
Because of the way my /votes/create.js.rjs works it send the result to div id="votes", and in the <% @posts.each do |post| %> I wrap <%= post.votes.count %> in the votes div tag. But when create.js.rjs sends the data, it only updates the first div tag on the rendered html page.
So if you can imagine, like on facebook, when you click that you like something, the update happens not on the post you just clicked but back at the top of the page on another post, the first post. The vote count is represented from the element you clicked but it is on the wrong record.
Here is the code:
POSTS/INDEX.HTML.ERB
<% @posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => @post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="votebutton">
<% remote_form_for [ post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= submit_tag '', :class => 'voteup' %>
</div>
<div id="vote"><br/>
<%= post.votes.count %> People like the above BattleCry. <br/>
<%= link_to "Comments (#{post.comments.count})", post %>
</div>
</div>
</div>
<br/>
<br/>
<br/>
<br/>
<% end %>
<% end %>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
/POSTS/CREATE.JS.RJS
page.replace_html :votes, :partial => @vote
page[@vote].visual_effect :highlight
/POSTS/_VOTE.HTML.ERB
<% div_for vote do %>
<%= @post.votes.count %> with your vote. Awesome!!!
<div id="commenttime">
Thanks for your vote. <br/>
</div>
<% end %>