LikesControler for a Post
Some notes:


Use 2-space indentation.
Move all the logic to the model.
DB deletes can also fail.
No need for self.
If you use object instead of object_id as argument, code is usually more declarative.
In case of error, you are performing a double render.


I'd write:

class LikesController &lt; ApplicationController
  def create
    post = Post.find(params[:post_id])

    if current_user.toggle_like(post)
      render(json: {status: "success", likes: post.likes.count})
    else
      render(json: {status: "error"}, status: :unprocessable_entity)
    end
  end
end

class User &lt; ActiveRecord::Model
  # Toggle like for post. Returns boolean with the status of the operation.
  def toggle_like(post)
    likes_for_post = likes.where(post_id: post.id)

    if likes_for_post.exists?
      likes_for_post.destroy_all.all?(&amp;:destroyed?)
    else
      likes.new(post: post).save
    end
  end
end