如果我先放置表单,则路由问题

时间:2012-10-01 23:26:00

标签: ruby-on-rails ruby-on-rails-3

我按照michale hartl的rails教程,然后我想扩展它,所以我试图整合thumbs_up gem。我能够为这两个帖子和评论投票,并感到非常自豪。但后来我想在我的UI上工作一点点。我不喜欢添加新评论的所有表单都低于帖子附带的评论提要。所以我认为我所要做的就是改变我的部分顺序

来自这个

<%= render partial: 'comments/comment', collection: my_item.comments, as: :comment %>
<%= render :partial => "comments/form", :locals => { :cur_post => my_item } %>

到这个

<%= render :partial => "comments/form", :locals => { :cur_post => my_item } %>
<%= render partial: 'comments/comment', collection: my_item.comments, as: :comment %>

它会起作用。但遗憾的是并非如此。当我重新排序它时,我得到了这个错误。

No route matches {:action=>"vote_up", :controller=>"comments", :id=>#<Comment id: nil, content: nil, user_id: nil, post_id: 14, created_at: nil, updated_at: nil>}

我能够删除部分内容并且一切都会正常工作,所以我不确定为什么在注释上方使用表单会导致错误。

以下是指向gist

的链接

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

问题来自我相信的这条线

<%= form_for([cur_post, cur_post.comments.build]) do |f| %>

首先发生这种情况时,会在cur_post.comments上构建一个新的评论。现在出现问题,当您呈现“评论/评论”时,不保存新建的评论。当你将模型传递给这样的url helper时,Rails会期望:

vote_up_comment_path(comment)

该对象位于db(AFAIK)

你应该能够通过不构建comments assocaition

来解决这个问题
<%= form_for([cur_post, Comment.new]) do |f| %>

否则,在您的评论/评论中,您可以查看comment.new_record?是否为真,并据此处理。