自引用关联存在路由错误

发布于 2024-09-29 03:10:29 字数 1198 浏览 0 评论 0 原文

我正在关注 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])

应用程序在创建过程中失败,因为它从未进入创建方法的重定向部分。

I am following Ryan Bate's tutorial: http://railscasts.com/episodes/163-self-referential-association

But my setup is slightly different.

I am making comments that are self-referential so that comments can be commented on.

The form displays in the view, but when I submit, I get this :

Routing Error

No route matches "/conversations"

And my url says this : 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

My form :

- 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'

My conversations_controller:

def create
  @conversation = comment.conversations.build(:convo_id => params[:convo_id])

The app fails here in its creation as it never makes it to the redirect portion of the create method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-06 03:10:29

这里有几个部分需要解决,但好消息是我认为您正在寻找的答案比您已有的答案更简单。如果我理解正确的话,您希望评论拥有许多自己的子评论。这就是 YouTube 的运作方式,让会员回复现有评论。为此,您不需要已实现的 has_many :through 解决方案。您根本不需要对话对象。一条评论可能有很多回复(子评论),但一条回复不会有多个父评论。

答案是使用多态性,这比发音更容易实现:) 您希望您的评论要么属于一个帖子,要么属于另一个评论。多态性让一个对象可能属于多种事物之一。事实上,评论是最常见的用途。

我用这篇博文中的地址示例介绍了多态性:

http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

但我可以向您展示如何做到这一点更具体地适用于您的情况。首先,完全放弃对话模型/控制器/路由。然后,更改您的评论表:

change_table :comments do |t|
  t.integer :commentable_id
  t.string  :commentable_type

  t.remove :post_id
end

我们不再需要 post_id,因为我们将更改与其他表关联的方式。现在让我们更改模型:

# app/models/post.rb
has_many :comments, :as => :commentable

# app/models/comment.rb
belongs_to :commentable, :polymorphic => true
has_many :comments, :as => :commentable

请注意,我们直接删除了属于帖子的评论。相反,它连接到多态的“可评论”关联。现在您可以无限深度地发表评论。

现在,在您的 Post#show 操作中,您需要创建一个空白评论,如下所示:

get show
  @post = Post.find(params[:id])
  @comment = @post.comments.build
end

@comment 现在将设置 commentable_idcommentable_type自动为您服务。现在在您的显示页面中,使用 erb:

<% form_for @comment do |f| %>
  <%= f.hidden_field :commentable_type %>
  <%= f.hidden_field :commentable_id %>

  /* other fields go here */
<% end %>

现在,当调用 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:

change_table :comments do |t|
  t.integer :commentable_id
  t.string  :commentable_type

  t.remove :post_id
end

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:

# app/models/post.rb
has_many :comments, :as => :commentable

# app/models/comment.rb
belongs_to :commentable, :polymorphic => true
has_many :comments, :as => :commentable

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:

get show
  @post = Post.find(params[:id])
  @comment = @post.comments.build
end

@comment will now have commentable_id and commentable_type set for you, automatically. Now in your show page, using erb:

<% form_for @comment do |f| %>
  <%= f.hidden_field :commentable_type %>
  <%= f.hidden_field :commentable_id %>

  /* other fields go here */
<% end %>

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!

心碎无痕… 2024-10-06 03:10:29

该应用程序失败的速度比您想象的要快 - 它没有找到执行该操作的路径,因此根本无法执行该操作。在您的routes.rb 文件中,您需要添加:

# rails 3
resources :conversations

# rails 2
map.resources :conversations

这应该可以修复它。

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:

# rails 3
resources :conversations

# rails 2
map.resources :conversations

This should fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文