使用 Rails 3,我在根命名空间中有一些模型(例如 Product),但在 Admin::模块中有 Controller (例如 Admin::ProductsController)。不幸的是,这似乎会导致基本功能出现问题,例如 form_for(@product) 生成
undefined method `products_path' for #<#<Class:0x103dc4110>:0x103dc1618>
这大概是因为 routes.rb 条目
namespace :admin do
resources :products
end
生产
admin_products GET /admin/products(.:format) {:action=>"index", :controller=>"admin/products"}
带有“admin_”前缀,但 form_for(@product) 似乎不明白这一点。
我只想向 Rails 声明编辑产品的 Controller 是 Admin::ProductsController,但似乎缺少两者之间的链接。我试过了:
构建这种系统的最接近 Rails 的现代方法是什么,我该如何解决这个问题?我觉得我一直在与框架作斗争,所以如果有一个整体更好的方法来做到这一点,我愿意接受建议,但即使我试图通过将模型放在 Admin 中来完全绕过问题:::只是开辟了新问题…
最佳答案
这应该做你想做的:form_for [:admin, @product]
关于namespaces - 协调 Admin:: Controller 与::模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3721735/