AJAX vote update for a nested resource

11 views
Skip to first unread message

Sean Six

unread,
Oct 13, 2011, 8:31:36 PM10/13/11
to rubyonra...@googlegroups.com
I created a house scaffold which has many reviews and the reviews can be
voted on. All the reviews for a house are shown on the house show page.
Then each review has a vote button and a display to show the current
number of votes. I would like the vote button to vanish and the vote
count to change by AJAX after each vote.


# reviews/_rev.html.erb
<% @house.reviews.each do |review| %>
posted <%= time_ago_in_words review.created_at %> ago by <%=
review.user.username %> |
<p id='votes'><%= pluralize(review.votes_count, 'vote') %></p>

<% if current_user %>
<%= form_for(@vote, :remote => true) do |f| %>
<%= f.hidden_field "review_id", :value => review.id %>
<%= f.hidden_field "user_id", :value => current_user.id %>

<%= f.submit "vote" %>
.
.
.

#votes controller
def create

r = params[:vote][:review_id]
@review = Review.find(r)
@vote = @review.votes.build(params[:vote])
@vote.user_id = current_user.id

respond_to do |format|
if @vote.save

format.js { }

#create.js.erb
$(document).ready(function(){
alert('voted');

});


$("#votes").html("<%= review.votes_count %>")


As it stands now, the vote will be posted but the AJAX will not work.
It will say undefined variable 'review'.

--
Posted via http://www.ruby-forum.com/.

Tim Shaffer

unread,
Oct 14, 2011, 8:04:33 AM10/14/11
to rubyonra...@googlegroups.com
You're defining @review as an instance variable, so you have to use it as one in the view:

$("#votes").html("<%= @review.votes_count %>")

Sean Six

unread,
Oct 14, 2011, 2:29:20 PM10/14/11
to rubyonra...@googlegroups.com
That doesn't quite work. When I use <%= @review.votes_count %> in the
create.js.erb file it updates the counter on the first review,
regardless of whether the vote was placed on the first review or not.

I thought maybe if I loop through the reviews again the create.js.erb
file it might work.

<% @house.reviews.each do |review| %>

$("#votes").html("<%= review.votes_count %>")

<% end %>

This did not work either.

Tim Shaffer

unread,
Oct 14, 2011, 2:50:42 PM10/14/11
to rubyonra...@googlegroups.com
That's to be expected with your HTML.

What you currently have will generate HTML that looks like the following if you have 3 reviews:

<p id='votes'>1 vote</p>
<p id='votes'>2 votes</p>
<p id='votes'>3 votes</p>

$("#votes").html("3 votes")
$("#votes").html("4 votes")
$("#votes").html("5 votes")

See the problem?

The problem is that each paragraph tag has the same ID. jQuery will only update the first one. Each paragraph needs to have a unique ID, then your JavaScript to update the paragraph needs to update the specific unique ID.

Including the primary key in the HTML ID is a good solution:

<p id="review_<%= review.id %>">

Then your jQuery can update the specific paragraph that it needs to:

$("#review_<%= review.id %>").html("<%= review.votes_count %>")


Sean Six

unread,
Oct 14, 2011, 6:13:46 PM10/14/11
to rubyonra...@googlegroups.com
I'm not sure why it still isn't working for me.

<p id="review_<%= review.id%>"><%= pluralize(review.votes_count, 'vote')
%></p>


create.js.erb
$("review_<%= @review.id %>").html("<%= @review.votes_count %>")

I don't see any errors in the log.

Sean Six

unread,
Oct 15, 2011, 10:27:07 PM10/15/11
to rubyonra...@googlegroups.com
Solution:

<% @house.reviews.each do |review| %>

$("#review_<%= review.id %>").html("<%= pluralize(review.votes_count,
'vote') %>");
<% end %>

Reply all
Reply to author
Forward
0 new messages