M
M
Max2016-07-05 14:33:49
Ruby on Rails
Max, 2016-07-05 14:33:49

How to find out if a Rails user voted (acts_votable gem)?

Hello, I have the following condition:

- if user_signed_in?
    - if current_user.voted_for? answer and answer.liked_by current_user
      = link_to unlike_answer_path(answer), class: "unlike", remote: true, method: :put do
        button.btn.btn-info aria-label=("Left Align") type="button"
          span.glyphicon.glyphicon-thumbs-down.glyphicon-align-center aria-hidden="true"
          span.badge  id="badge_downvote_value" = answer.get_downvotes.size
    - elsif current_user.voted_for? answer and answer.disliked_by current_user
      = link_to like_answer_path(answer),  class: "like", remote: true,  method: :put do
        button.btn.btn-info aria-label=("Left Align") type="button"
          span.glyphicon.glyphicon-thumbs-up.glyphicon-align-center aria-hidden="true"
          span.badge id="badge_upvote_value" = answer.get_upvotes.size

First, I check if the user is logged in, and then if he voted and liked the answer, I hide the like button and display the dislike button, otherwise, on the contrary, I hide the dislike button and display the button for like. BUT, for reasons I don't understand, only the dislike button is constantly displayed. and it doesn't work. That is, when you try to put a dislike, nothing happens .. If you remove the conditions and leave just 2 buttons, then everything works fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Korablin, 2016-07-05
@maxprof

In the second line
- if current_user.voted_for? answer and answer.liked_by current_user
you do the same thing - check if the user has voted. Do it
like this
- if current_user.voted_up_on? @answer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question