Skip to content

Upvote and downvote a Scenario

Anoob Bava edited this page Jun 4, 2016 · 6 revisions

Steps

install the Gem

find the model to which up voting and down voting to be applied

if we have the model Post model class Post < ApplicationRecord acts_as_votable end

Update the routes

now we need to update the routes resources :posts do member do put "like", to: "posts#upvote" put "dislike", to: "posts#downvote" end end

create 2 custom methods

def upvote
 @post.upvote_by current_user
 redirect_to @post
end
def downvote
 @post.downvote_by current_user
 redirect_to @post
end

Now we need to update the views

<%=link_to like_post_path(@post), method: :put, class: 'btn btn-default btn-sm' do %> like <%=@post.get_upvotes.size%> <% end %> <%=link_to dislike_post_path(@post), method: :put, class: 'btn btn-default btn-sm' do %> dislike <%=@post.get_downvotes.size%>