Rails Acts_as_votable Gem Like / Unlike Ajax Buttons

I am new to Ruby On Rails, I used action_as_votable gem to create Like and Unlike Buttons so that users might not like users, but I can’t get them to change from Like to Unlike (and vice versa) and update the counter every time they click without refreshing the page. I tried other similar answers, but no luck. Without the messy changes I was trying to make to implement Ajax, my code looked like this:

Post Model act_as_votable and User Model act_as voter

Message controller has

def like
  @post = Post.find(params[:id])
  @post.liked_by current_user
  redirect_to :back
end

def unlike
  @post = Post.find(params[:id])
  @post.unliked_by current_user
  redirect_to :back
end

Routes have

resources :posts do
  member do
    put 'like', to: "posts#like"
    put 'unlike', to: "posts#unlike"
  end
end

View has

<%= @post.get_likes.size%>
  <% if @post.get_likes.size ==1 %>
    person like this
  <% else %>
   people like this
<% end %>


<div class="btn-group">

  <% if (current_user.liked? @post) %>
    <%= link_to unlike_post_path(@post), method: :put, class: "btn btn-default btn-sm" do %>
    <span class="glyphicon glyphicon-chevron-down"></span>
      Unlike
    <%end %>

  <% else %>

    <%= link_to like_post_path(@post), method: :put, class: "btn btn-primary btn-sm" do %>
    <span class="glyphicon glyphicon-chevron-up"></span>
      Like
    <% end %>
  <% end %>

</div>

I read a lot of answers about Ajax, but I was not able to reproduce the results. Thanks in advance!

+4
source share
1 answer

-, js-. posts_controller :

def like
  @post = Post.find(params[:id])
  @post.liked_by current_user
  respond_to do |format|
    format.html { redirect_to :back }
    format.js { render layout: false }
  end
end

def unlike
  @post = Post.find(params[:id])
  @post.unliked_by current_user
  respond_to do |format|
    format.html { redirect_to :back }
    format.js { render layout: false }
  end
end

-, remote: true :

 <div class="votes">
    <% if current_user.liked? @post %>
       <%= link_to unlike_post_path(@post), method: :get, remote: true, class: 'unlike_post' %>
     <% else %>
       <%= link_to like_post_path(@post), method: :get, remote: true, class: 'like_post' %>
     <% end %>
  </div>

method: :put method: :get, config/routes.rb , js.

, 2 app/views/posts/:

  • like.js.erb

    $('.like_post').bind('ajax:success', function(){
       $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @post.votes_for.size.to_s %>');
       $(this).closest('.like_post').hide();
       $(this).closest('.votes').html(' <%= link_to "Unlike", unlike_post_path(@post), remote: true, method: :get, class: 'unlike_post' %>');
    });
    
  • unlike.js.erb

    $('.unlike_post').bind('ajax:success', function(){
       $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @post.votes_for.size.to_s %>');
       $(this).closest('.unlike_post').hide();
       $(this).closest('.votes').html(' <%= link_to "Like", like_post_path(@post), remote: true, method: :get, class: 'like_post' %>');
    
    });
    

, .vote_count, :

<div class="vote_count">
  <%= @post.get_likes.size %>
</div> 

, :

<div>
  <div class="vote_count">
    <%= @post.get_likes.size %>
  </div> 

  <div class="votes">
    <% if current_user.liked? @post %>
      <%= link_to unlike_post_path(@post), method: :get, remote: true, class: 'unlike_post' %>
    <% else %>
      <%= link_to like_post_path(@post), method: :get, remote: true, class: 'like_post' %>
    <% end %>
  </div>
</div>

: . id. 2 js, (). sandbox. .

+1

All Articles