Skip to content

Instantly share code, notes, and snippets.

@dgorodnichy
Last active July 16, 2020 12:25
Show Gist options
  • Save dgorodnichy/c013cef8081b3adf22c5ba538214c6bd to your computer and use it in GitHub Desktop.
Save dgorodnichy/c013cef8081b3adf22c5ba538214c6bd to your computer and use it in GitHub Desktop.
module Api
class LikesController < ApplicationController
def update
@user = User.find(params['user_id'])
@post = Post.find(params['post_id'])
like = @post.likes.find_or_initialize_by(user: @user)
if like.persisted?
like.destroy!
render json: { isLiked: false }
else
like.save!
render json: { isLiked: true }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment