I am creating a Rails 4 app in which a user can upvote a given post through the acts_as_votable gem. Let's say as a user I press the upvote button, how would I make it so that if I press the SAME button again, the vote gets taken away, so in essence an "unupvote"?
Here is what my upvote method looks like in my posts controller:
def upvote
@post = Post.find(params[:id])
@post.upvote_by current_user
redirect_to :back
end
My post model
class Post < ActiveRecord::Base
acts_as_votable
belongs_to :user
end
And finally this how I am rendering the button in my views
<div class="btn-group">
<%= link_to like_post_path(post), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= post.get_upvotes.size %>
<% end %>
</div>
Thanks for the help guys!