acts_as_votable thumbs up/down buttons

后端 未结 2 1793
慢半拍i
慢半拍i 2020-12-08 12:46

I installed the acts_as_votable gem, it works in the console like it should (like it says in the documentation). So my question is how to set up a form for upvote and downvo

2条回答
  •  执念已碎
    2020-12-08 13:22

    One way to do this is to add your own controller actions for up- and downvotes. I'm assuming you have a current_user method available in your controller.

    # pictures_controller.rb
    def upvote
      @picture = Picture.find(params[:id])
      @picture.liked_by current_user
      redirect_to @picture
    end
    
    def downvote
      @picture = Picture.find(params[:id])
      @picture.downvote_from current_user
      redirect_to @picture
    end
    
    # config/routes.rb
    
    resources :pictures do
      member do
        put "like", to: "pictures#upvote"
        put "dislike", to: "pictures#downvote"
      end
    end
    
    # some view
    
    <%= link_to "Upvote", like_picture_path(@picture), method: :put %>
    <%= link_to "Downvote", dislike_picture_path(@picture), method: :put %>
    

提交回复
热议问题