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/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Tim Shaffer at Oct 14, 2011 at 12:04 pm
    You're defining @review as an instance variable, so you have to use it as
    one in the view:

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Ocd3vOdvJUwJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Sean Six at Oct 14, 2011 at 6:29 pm
    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.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tim Shaffer at Oct 14, 2011 at 6:50 pm
    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 %>")


    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8XDnjkoJ3SUJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Sean Six at Oct 14, 2011 at 10:14 pm
    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.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Sean Six at Oct 16, 2011 at 2:27 am
    Solution:

    <% @house.reviews.each do |review| %>
    $("#review_<%= review.id %>").html("<%= pluralize(review.votes_count,
    'vote') %>");
    <% end %>

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 14, '11 at 12:31a
activeOct 16, '11 at 2:27a
posts6
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Sean Six: 4 posts Tim Shaffer: 2 posts

People

Translate

site design / logo © 2021 Grokbase