Problem: Looking for the right comment that belongs to a post
I am trying to implement a "like" function (just like on facebook) for comments on a specific post. I've already implemented the same function for my posts, but "pointing at the right comment" is giving me hard time. To clarify, my "like" function results in the following GET call:
http://localhost:3000/posts/11/comments/4/like
But it's actually supposed to call
/posts/4/comments/11/like
I checked my route, and it seems right to me
like_post_comment GET /posts/:post_id/comments/:id/like(.:format)
So I guess the problem lies in the controller.
In the beginning of my like action in comments_controller, I have
def like
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:comment])
I think this must be wrong, but I'm not sure why or how to fix it. The other actions set local variables @post and @comment in a similar way, but they do the job correctly.
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:comment])
How I render my link to comment
<td><b><%= link_to 'like', like_post_comment_path(comment) %></b></td>
Replace your link from
<td><b><%= link_to 'like', like_post_comment_path(comment) %></b></td>
to
<td><b><%= link_to 'like', like_post_comment_path(@post, comment) %></b></td>
And replace your like action in controller with
def like
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
# ...
end