Skip to content

Instantly share code, notes, and snippets.

@meoooh
Created August 28, 2013 00:44
Show Gist options
  • Save meoooh/6360853 to your computer and use it in GitHub Desktop.
Save meoooh/6360853 to your computer and use it in GitHub Desktop.
#POST /posts/:id/like
#POST /posts/1/like
def like
if @post = Post.where(id: params[:id]) #일단 POST 모델을 가져옴...
if @like = LikePost.where(user_id: current_user.id, post_id: @post.id).empty? #그다음 LikePost모델에 현재 post_id와 user_id를 검색하여 비었을때(empty)는 좋아요(like)
@like = LikePost.new(user_id: current_user.id, post_id: @post.id) # 현재 post와 현재 user로 새로운 포스트 작성
if @like.save
redirect_to @post
else
redirect_to @post
end
else # 만약 LikePost에 검색되면 empty?에 false니 빈게 아니므로 이미 좋아요 되어있으므로 해당 객체 제거...
@like.destroy
redirect_to @post
end
else
redirect_to posts_url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment