1

I have some really strange situation right here. When I log in user all posts get liked by that user. Here are my routes:

  resources :posts do
    member do
      put 'upvote', to: 'posts#upvote'
      put 'unvote', to: 'posts#unvote'
    end
  end

Here is view:

  <% if logged_in? %>
    <% if post.liked_by current_user %>
      <%= link_to "Dislike", unvote_post_path(post), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: upvote_post_path(post), id: post.id } %>
    <% else %>
      <%= link_to "Like", upvote_post_path(post), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: unvote_post_path(post), id: post.id } %>
    <% end %>
  <% end %>

and here is controller:

  def upvote
    @post = Post.find(params[:id])
    @post.liked_by current_user
    if request.xhr?
      render json: { count: @post.get_likes.size, id: params[:id] }
    else
      redirect_to @post
    end
  end

  def unvote
    @post = Post.find(params[:id])
    @post.unliked_by current_user
    if request.xhr?
      render json: { count: @post.get_likes.size, id: params[:id] }
    else
      redirect_to @post
    end
  end

I use gem https://github.com/ryanto/acts_as_votable and I added some ajax interaction following this: Like button Ajax in Ruby on Rails

Community
  • 1
  • 1
Kunok
  • 8,089
  • 8
  • 48
  • 89
  • Oh it probably is because of this condition <% if post.liked_by current_user %> I will check if that is it. – Kunok Mar 16 '16 at 20:45

1 Answers1

0

The issue was in view if statement <% if post.liked_by current_user %> which made user like these posts instead of being boolean method. I switched it with <% if current_user.voted_for? post %> and added acts_as_voter to User model and it works fine now.

Kunok
  • 8,089
  • 8
  • 48
  • 89