自引用关联存在路由错误
我正在关注 Ryan Bate 的教程: http://railscasts.com/episodes/163-self-参考关联
但我的设置略有不同。
我正在发表自我参考的评论,以便评论可以被评论。
表单显示在视图中,但是当我提交时,我得到:
Routing Error
No route matches "/conversations"
我的网址是这样的: http://localhost:3000/conversations?convo_id=1
models
#conversation.rb
belongs_to :comment
belongs_to :convo, :class_name => "Comment"
#comment.rb
belongs_to :post
has_many :conversations
has_many :convos, :through => :conversations
我的表单:
- for comment in @comments
.grid_7.post.alpha.omega
= comment.text
%br/
- form_for comment, :url => conversations_path(:convo_id => comment), :method => :post do |f|
= f.label 'Comment'
%br/
= f.text_area :text
%br/
= f.submit 'Submit'
我的对话控制器:
def create
@conversation = comment.conversations.build(:convo_id => params[:convo_id])
应用程序在创建过程中失败,因为它从未进入创建方法
的重定向部分。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有几个部分需要解决,但好消息是我认为您正在寻找的答案比您已有的答案更简单。如果我理解正确的话,您希望评论拥有许多自己的子评论。这就是 YouTube 的运作方式,让会员回复现有评论。为此,您不需要已实现的
has_many :through
解决方案。您根本不需要对话对象。一条评论可能有很多回复(子评论),但一条回复不会有多个父评论。答案是使用多态性,这比发音更容易实现:) 您希望您的评论要么属于一个帖子,要么属于另一个评论。多态性让一个对象可能属于多种事物之一。事实上,评论是最常见的用途。
我用这篇博文中的地址示例介绍了多态性:
http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/
但我可以向您展示如何做到这一点更具体地适用于您的情况。首先,完全放弃对话模型/控制器/路由。然后,更改您的评论表:
我们不再需要
post_id
,因为我们将更改与其他表关联的方式。现在让我们更改模型:请注意,我们直接删除了属于帖子的评论。相反,它连接到多态的“可评论”关联。现在您可以无限深度地发表评论。
现在,在您的 Post#show 操作中,您需要创建一个空白评论,如下所示:
@comment
现在将设置commentable_id
和commentable_type
自动为您服务。现在在您的显示页面中,使用 erb:现在,当调用 Comments#create 时,它会像您期望的那样工作,并附加到正确的父级。上面的示例显示了直接添加到帖子中的评论,但评论的过程本质上是相同的。在控制器中,您可以调用
@comment.comments.build
,并且表单本身将保持不变。我希望这有帮助!
There are several pieces to work on here, but the good news is I think the answer you're looking for is simpler than what you already have. If I understand you correctly, you want comments to have many child comments of their own. This is how YouTube works, letting members reply to existing comments. For this, you don't need the
has_many :through
solution you've implemented. You don't need the conversations object at all. A comment may have many replies (child comments), but a reply isn't going to have more than one parent.The answer for this is using polymorphism, which is easier to implement than it is to pronounce :) You want your comments to either belong to a post, or to another comment. Polymorphism lets an object belong to one of possibly many things. In fact, comments are the most common use for this.
I cover polymorphism with the example of addresses in this blog post:
http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/
But I can show you how it applies to your case more specifically. First, drop the conversation model/controller/routes entirely. Then, change your comments table:
We don't need
post_id
anymore, because we're going to change how we associate with other tables. Now let's change the models:Notice we dropped the comment belonging to a post directly. Instead it connects to the polymorphic "commentable" association. Now you have an unlimited depth to comments having comments.
Now in your Post#show action, you'll want to create a blank comment like so:
@comment
will now havecommentable_id
andcommentable_type
set for you, automatically. Now in your show page, using erb:Now when Comments#create is called, it works like you'd expect, and attaches to the right parent. The example above was showing a comment being added directly to a post, but the process is essentially the same for a comment. In the controller you'd call
@comment.comments.build
, and the form itself would stay the same.I hope this helps!
该应用程序失败的速度比您想象的要快 - 它没有找到执行该操作的路径,因此根本无法执行该操作。在您的routes.rb 文件中,您需要添加:
这应该可以修复它。
The app is failing sooner than you think - it's not finding a route to that action, so it's not reaching it at all. In your routes.rb file, you need to add:
This should fix it.