-
Notifications
You must be signed in to change notification settings - Fork 217
Upvote and downvote a Scenario
John Knapp edited this page Mar 6, 2018
·
6 revisions
gem install acts_as_votable
rails generate acts_as_votable:migration
rake db:migrate
if we have the model Post model
class Post < ApplicationRecord
acts_as_votable
end
now we need to update the routes
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
end
def upvote
@post.upvote_by current_user
redirect_to @post
end
def downvote
@post.downvote_by current_user
redirect_to @post
end
Note: You probably want to add these two methods to your before_action
callback. If you're using CanCanCan, you may want to add these upvote and downvote methods there too.
<%=link_to like_post_path(@post), method: :put, class: 'btn btn-default btn-sm' do %>
<span class="glyphicon glyphicon-chevron-up"></span>
like <%=@post.get_upvotes.size%></td>
<% end %>
<%=link_to dislike_post_path(@post), method: :put, class: 'btn btn-default btn-sm' do %>
<span class="glyphicon glyphicon-chevron-down"></span>
dislike <%=@post.get_downvotes.size%></td>
<%end%>