Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `voted_on?' for nil:NilClass error message using thumbs_up
    text
    copied!<p>I'm currently trying to implement likes and unlikes rails app using thumbs_up. I followed the instructions on this page: <a href="https://stackoverflow.com/questions/4907744/clarification-on-how-to-use-thumbs-up-voting-gem-with-rails-3">Clarification on how to use &quot;thumbs_up&quot; voting gem with Rails 3</a></p> <p>Users can like and unlike books. I have 2 buttons, like and unlike and I would like to hide one or the other from the user, depending on the users current like status. So I figured an if else would be appropriate like this: </p> <pre><code>&lt;% if @user.voted_on?(@book) %&gt; &lt;div class="unlike_button"&gt;&lt;%= link_to("Unlike", unvote_book_path(book), method: :post) %&gt;&lt;/div&gt; &lt;% else %&gt; &lt;div class="like_button"&gt;&lt;%= link_to("Like", vote_up_book_path(book), method: :post) %&gt;&lt;/div&gt; &lt;% end %&gt; </code></pre> <p>and in my route.rb file:</p> <pre><code>resources :books do member do post :vote_up post :unvote end end </code></pre> <p>But when I run this I get the error message:</p> <blockquote> <p>undefined method `voted_on?' for nil:NilClass</p> </blockquote> <p>Is there anything I might be doing wrong?</p> <p><em>Update</em></p> <p>As Mischa suggested, I changed it to current_user.voted_on. Now I get this error message:</p> <pre><code>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id </code></pre> <p>Below is a snippet of my Books controller</p> <pre><code>include UsersHelper include SessionsHelper before_filter :signed_in_user, only: [:index] before_filter :admin_user, only: :destroy def index array = Book.search(params[:search]) @books = Kaminari.paginate_array(array).page(params[:page]).per(5) end def show @book = Book.find(params[:id]) #respond_to do |format| #format.js #end end def destroy Book.find(params[:id]).destroy flash[:success] = "Book deleted." redirect_to books_url end def vote_up begin current_user.vote_for(@book = Book.find(params[:id])) flash[:success] = "Liked!." redirect_to books_url end end def unvote begin current_user.unvote_for(@book = Book.find(params[:id])) flash[:success] = "Unliked!." redirect_to books_url end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload