将一个模型与另一个模型关联而无需嵌套资源

发布于 2024-12-16 19:22:25 字数 591 浏览 2 评论 0原文

假设我有一个帖子模型和一个评论模型,在路由文件中我声明

resources :posts
resources :comments

NOT

resources :posts do
  resources :comments
end

Also:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :comments
end

class Comment < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

我不知道如何:

  1. 创建一个新表单,我可以在其中创建帖子及其相关评论一次。

    1a。视图是什么样的?

    1b。什么控制器里有什么?

  2. 在帖子的显示页面中创建一个表单,向该帖子添加评论。

    2a。帖子显示页面上添加评论的表单的视图是什么样的?

    2b。我要向哪个控制器添加什么?

Let's say I have a Post model and a Comments model where in the routes file I'm declaring

resources :posts
resources :comments

NOT

resources :posts do
  resources :comments
end

Also:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :comments
end

class Comment < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

I can't figure out how to:

  1. Create a new form where I can create the Post and its associated Comments at once.

    1a. What does the view look like?

    1b. What goes in what controller?

  2. Create a form in a post's show page that adds a comment to that post.

    2a. What does the view look like for the form on the post show page that adds a comment to it?

    2b. What do I add to what controller?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文