如何让 form_for 为嵌套资源工作 - 有一点小小的改变
我希望在其自己的表单上添加/编辑子模型,而不是在父表单中。
我见过的任何示例都将子级添加到与父级相同的表单中。由于我在单独的表单上添加了添加/编辑的功能,我想知道这是否就是我收到以下错误的原因:
未定义的方法`chapters_path'...
routes.rb:
resources :books do
resources :chapters
end
book。 rb:
has_many :chapters
chapter.rb
belongs_to :book
_form.html.erb:
<% form_for [@book, @chapter] do |f| %>
我认为我仍然需要使用嵌套资源方法,因为我想添加具有诸如 books/1/chapters/new 之类的 url 的子项 - 因为那将是正确的 Rails 方法,不是吗?或者也许我以错误的方式看待这个问题。
非常感谢任何帮助。
I'm looking to have the child model added/edited on it's own form, not within the parent form.
any example I have seen has the child added on the same form as the parent. Since I'm adding the twist of adding/editing on a separate form, I'm wondering if that's why I get the following error:
undefined method `chapters_path'...
routes.rb:
resources :books do
resources :chapters
end
book.rb:
has_many :chapters
chapter.rb
belongs_to :book
_form.html.erb:
<% form_for [@book, @chapter] do |f| %>
I think I still need to use the nest resource methodology, since I'm wanting to add the child with a url such as books/1/chapters/new - because that would be the proper rails approach, no? or maybe I'm looking at this the wrong way.
Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的 - 我已经开始工作了 - 如果有人感兴趣,这里是调整:
routes.rb调整
章节控制器中的一些重定向也需要进行
ok - I've got it working - in case anyone is interested, here are the adjustments:
routes.rb
adjustments were also required for some of the redirects in the chapters controller