简体   繁体   中英

acts_as_votable gem likes all the posts on page in rails

Lemme explain what I mean, the point is that I am using acts_as_votable gem in my rails app and it works fyn, problem is that I am using ajax and I am using it to like individual posts from my index page . I will explain with the code

This is my @confessions controller instance variable that has all the votes

 @confessions = Confession.where(amitian_id: ids).order('created_at DESC')

Now this is my view that shows all the posts

  -@confessions.each do |c|
  # code  
  =link_to pluralize(c.get_upvotes.size,"Like"),like_confession_path(c) , method: :get,remote: true , class: 'like'
  =link_to pluralize(c.get_downvotes.size,"Dislike"),dislike_confession_path(c) , method: :get,remote: true ,class: 'dislike'
  end

Well, up until now I can use 'c' variable to refer to a single post.. but in my ajax script I have something like this

  $('.like').html('<%=  pluralize(@confessions.get_upvotes.size,"Like") %>');
  $('.dislike').html('<%=  pluralize(@confessions.get_downvotes.size,"Dislike") %>');

Now obviously, this will like all the post variable @confessions have.. how can i write a script to make it like the post user clicks on I suppose I have to use this or self keyword but m not sure how.. help plz

Move the confessions block to a separate partial. Then wrap that partial in a div. Then in your ajax script you can just reload the partial.

View:

<div id=confessions>
  <%= render 'confessions' %>
</div>

Ajax

$('#confessions').html("<%= escape_javascript(render 'confessions') %>")

Also, are you sure you want to be making GET requests to like/dislike? I think you should be making a POST request here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM