Allowing users to 'toggle' up_vote and down_vote using thumbs_up gem in Rails

Viewed 80

I'm using the thumbs_up gem to allow 'likes' in my app.

The gem includes this code to make sure that one user cannot vote twice -

Code '1'
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]

But I want my users to be able to 'up_vote' once, then 'down_vote' if they happen to like by mistake (for user experience's sake). I tried to change the scope to:

validates_uniqueness_of :voteable_id, :scope => [:voteable_type]

But that would allow users to 'up_vote' twice in a row. Users, instead, should only be able to 'down_vote' if the voteable object is already 'up_vote'd, and vice versa.

I can think of doing this by using a conditional in my view, as such:

<% if !current_user.voted_for?(voteable_name) %>
     <%= link_to('Like', vote_up_question_answer_path(voteable_name), :method => :post) %>
<% else %>
     <br>
     <%= link_to('Unlike', vote_down_question_answer_path(voteable_name), :method => :post) %>
<% end %>

And commenting out Code '1', but is there a better way to make users permitted to only 'toggle'-ing between the votes through the controller?

Thanks.

1 Answers
Related