将一个模型与另一个模型关联而无需嵌套资源
假设我有一个帖子模型和一个评论模型,在路由文件中我声明
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
我不知道如何:
创建一个新表单,我可以在其中创建帖子及其相关评论一次。
1a。视图是什么样的?
1b。什么控制器里有什么?
在帖子的显示页面中创建一个表单,向该帖子添加评论。
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:
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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一些文档。尝试以下资源:
阅读完它们后,您将能够解决所有问题。
You need some documentation. Try the following resources:
After reading them you'll be able to solve all your problems.