Learner
Learner

Reputation: 4636

how to find the current_user voted for the post? acts_as_votable rails

I have the following code in my view:

if current_user.voted_for? post => gives me undefined method voted_for?

How can I find the current_user is voted for the post?

Routes:

  resources :posts do
    resources :comments, only: [:index, :new, :create]
    member do
      get "like", to: "posts#upvote" 
      get "dislike", to: "posts#downvote" 
    end
  end

Controller:

def upvote
    @post.upvote_by current_user
    redirect_to :back
  end

  def downvote
    @post.downvote_by current_user
    redirect_to :back
  end

View:

  <% @posts.each do |post| %>
    <div class="vote-box">
      <p class="up">
        <% if post.voted_for? current_user %>
          <%= link_to(like_post_path(post), method: :get) do %>
            <i class="entypo-up-dir"></i>
          <% end %>
        <% else %>
          <%= link_to(dislike_post_path(post), method: :get) do %>
            <i class="entypo-up-dir upvoted"></i>
          <% end %>
        <% end %>
      </p>
      <p class="vote_count_text">
        <%= post.get_upvotes.size %>
      </p>
   </div>
 <% end %>

I tried current_user.likes? , voted_up_on? and several methods as per the gem tutorial but no luck.

Thanks

Upvotes: 0

Views: 903

Answers (1)

steel
steel

Reputation: 12570

It looks like all of your missing methods are related to your User model, but not all of the methods you're trying to use exist. First, make sure you have mixed in the acts_as_voter to your model.

class User < ActiveRecord::Base
  acts_as_voter
end

From the docs here.

Second, @user.likes? is not a method.

Upvotes: 3

Related Questions