Вопрос

I have petition site. I building now the voting system, but thats don't work glad. To create only one vote per user I use this condition in view:

<% if @post.votes.where(user_id: current_user.id).blank? %> 

It's return true if user do not vote for current post.

But when i want to show user vote statement with this code:

<% if @post.votes(user_id: current_user.id) == 1 %>
        "u voted LIKE"
       <% else %>
        "u voted DISLIKE"
       <% end %>

it return me the error: We're sorry, but something went wrong (500) I'm in development mode. Thanks.

Это было полезно?

Решение

You are missing where and count in the statement.

try this,

 <% if @post.votes.where(user_id: current_user.id).count.eql?(1) %>
    "u voted LIKE"
 <% else %>
    "u voted DISLIKE"
 <% end %>

Другие советы

Update the condition as

<% if @post.votes(user_id: current_user.id).count == 1 %>
        "u voted"
       <% else %>
        "u dont voted"
       <% end %>

You are counting the votes for that post of the user as 1 , if it is 1 then he has voted. else not voted.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top