1

I'm currently trying to implement likes and unlikes rails app using thumbs_up. I followed the instructions on this page: Clarification on how to use "thumbs_up" voting gem with Rails 3

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:

<% if @user.voted_on?(@book) %>    
  <div class="unlike_button"><%= link_to("Unlike", unvote_book_path(book), method: :post) %></div>
<% else %>
  <div class="like_button"><%= link_to("Like", vote_up_book_path(book), method: :post) %></div>
<% end %>

and in my route.rb file:

resources :books do
  member do
    post :vote_up
    post :unvote
  end
end

But when I run this I get the error message:

undefined method `voted_on?' for nil:NilClass

Is there anything I might be doing wrong?

Update

As Mischa suggested, I changed it to current_user.voted_on. Now I get this error message:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Below is a snippet of my Books controller

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
Community
  • 1
  • 1
Shikasan
  • 331
  • 1
  • 6
  • 20
  • `@user` is `nil`. Don't you mean `current_user.voted_on(@book)`? If you want to use `@user` you have to define it somewhere, which you apparently didn't do. – Mischa May 09 '13 at 03:40
  • can you post the rest of your controller action? – pungoyal May 09 '13 at 06:29
  • As Mischa Said, @user is nil here, You have to define it in controller or helper, or in even views also. But defining such variables in controller makes more sense. Or Just use current_user If you are using devise. – Jyothu May 09 '13 at 08:35
  • Hi, I changed it to current_user.voted_on(@book) and now my error message is: "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" – Shikasan May 09 '13 at 13:09

1 Answers1

-1

You should change your method of link from post to put and change your routes.rb to

resources :books

You can also update a data using a link by using this code:

 <%= link_to "Like", book, :method => :put %>
or 
 <%= link_to "Unlike", book, :method => :put %>

and this goes with your controller in assuming that you have a is_liked boolean field in your Post model

def update
@post = Post.find(params[:id])

   if @post.is_liked?
      @post.attributes = {
      :is_liked => "f"
    }
    redirect_to posts_path, :flash => "Unliked"
   else
     @post.attributes = {
      :is_liked => "t"
        }
     redirect_to posts_path, :flash => "Liked"
   end

end
AllenC
  • 2,754
  • 1
  • 41
  • 74