0

问题:寻找属于帖子的正确评论

我正在尝试为特定帖子的评论实现“喜欢”功能(就像在 facebook 上一样)。我已经为我的帖子实现了相同的功能,但是“指向正确的评论”让我很难过。澄清一下,我的“like”函数会导致以下 GET 调用:

http://localhost:3000/posts/11/comments/4/like

但它实际上应该调用

/posts/4/comments/11/like

我检查了我的路线,这对我来说似乎是正确的

like_post_comment GET    /posts/:post_id/comments/:id/like(.:format)

所以我想问题出在控制器上。

comments_controller中的点赞操作开始时,我有

def like
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:comment])

我认为这一定是错误的,但我不确定为什么或如何解决它。 其他操作以类似的方式设置局部变量@post@comment ,但它们正确地完成了工作。

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])

我如何呈现我的评论链接

<td><b><%= link_to 'like', like_post_comment_path(comment) %></b></td>
4

3 回答 3

1

像这样称呼它

<%= link_to 'like', like_post_comment_path(@post, comment) %>

其中@post 是当前的帖子对象

于 2012-11-21T08:26:01.677 回答
1

替换您的链接

<td><b><%= link_to 'like', like_post_comment_path(comment) %></b></td>

<td><b><%= link_to 'like', like_post_comment_path(@post, comment) %></b></td>

并将控制器中的类似操作替换为

def like
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  # ...
end
于 2012-11-21T08:44:54.897 回答
0

这个:

/posts/:post_id/comments/:id/like(.:format)

告诉我您的帖子由 post_id 参数标识,您的评论由 id 参数标识。因此,您的 like 方法应如下所示:

def like
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
于 2012-11-21T08:07:11.287 回答