Adding a “Like/Unlike” button to a post in Rails

前端 未结 4 2020
陌清茗
陌清茗 2021-02-04 22:34

The site is a simple community where each user creates posts and users may \"like\" them or \"unlike\" them.

I have a Post and a Like model. Currently, I\'m listing all

4条回答
  •  别跟我提以往
    2021-02-04 22:55

    Also you could add validation to your Like model like so:

    validate :user_does_not_already_like_post
    
    def user_does_not_already_like_post
      errors.add(:user, "You can only like a post once.") if user.already_likes?(post)
    end
    

提交回复
热议问题