命名空间 form_for 中的嵌套资源
问题
form_for 帮助程序错误地确定了命名空间内嵌套资源的路径。有问题的模型分别是: Forum::Thread 和 Forum::Reply ,分别位于我的模型目录下名为“forum”的子文件夹中。这是 Rails 3 BETA 3 中的内容。
routes.rb
namespace :forum do
root :to => 'threads#index'
resources :threads do
resources :replies
end
end
app/views/forum/replies/_form.html.haml
...
- form_for [@thread, @reply] do |f|
...
app/controllers/forum/replies_controller.rb
...
def new
@reply = Forum::Reply.new
end
...
错误
undefined method `forum_thread_forum_replies_path'
参考上面 _form.html.haml 中概述的行
Problem
The form_for helper incorrectly determines the path to my nested resource inside of a namespace. The models in question are: Forum::Thread and Forum::Reply respectively, located in a subfolder called "forum" under my models directory. This is in Rails 3 BETA 3.
routes.rb
namespace :forum do
root :to => 'threads#index'
resources :threads do
resources :replies
end
end
app/views/forum/replies/_form.html.haml
...
- form_for [@thread, @reply] do |f|
...
app/controllers/forum/replies_controller.rb
...
def new
@reply = Forum::Reply.new
end
...
Error
undefined method `forum_thread_forum_replies_path'
In reference to the line outlined above in _form.html.haml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑解决方案以防人们不阅读反应:
旧反应:
我有一个带有管理命名空间以及人员和图像资源的项目,这就是我构建我的项目的方式Rails3 中的 form_for ,我还没有找到一种更干净的方法......
Editted solution in case people don't read the reactions:
Old response:
I have a project with an admin namespace and People and Images resources, this is the way I build my form_for in rails3, I haven't found a way just yet to do it cleaner...
在 Rails 3 中,唯一对我正确有效的解决方案(对于新资源和编辑资源)是:
In Rails 3, the only solution that worked for me correctly (for both new and edit resource) was:
@Douglas:这对我不起作用。在我看来,路线中的名称应该是复数。当我确实喜欢推荐时,错误是:
我的解决方案适用于新:
我的解决方案适用于编辑:
是否有任何解决方案可以将其组合为一种形式?
编辑(表单中新的嵌套命名空间路由的解决方案):
现在我在routes.rb中有以下逻辑
new_mobile_user_candystore_transactions 的形式是
访问 Candystore::TransactionsController 创建方法,而不是访问 MobileUser 创建方法或 Candystore::TransactionsController 更新方法。
@Douglas: It's not working for me. In my view, the names in routes should be pluralize. When I do like recommended, the error is:
My solution that worked for New:
My solution that worked for Edit:
Ist there any solution to combine this in one form?
Edit (Solution for a new nested namespaced route in a form):
Now I had the following logic in the routes.rb
The form for new_mobile_user_candystore_transactions is
to get to the Candystore::TransactionsController create method and not to e.g the MobileUser create method or Candystore::TransactionsController update method.
上面的答案都不适合我 - 我有一个简单的类别/子类别关系,它由“媒体”名称空间包装。
所以我有命名空间的嵌套资源
,也有命名空间的模型
最后,我最终在子类别的 _form 中得到了这个:
None of the answers above worked for me - I have a simple Category / Subcategory relationship which is wrapped by a 'Media' namespace.
So I have namespaced nested resources
but also namespaced models
In the end, I ended up with this in the _form for subcategories: