开发者

Reconciling Admin:: controllers with :: models

开发者 https://www.devze.com 2023-01-16 13:27 出处:网络
Using Rails 3, I have some models (e.g. Product) in the root namespace, but controllers (e.g. Admin::ProductsController) in an Admin:: module. Unfortunately, this seems to be causing issues with basic

Using Rails 3, I have some models (e.g. Product) in the root namespace, but controllers (e.g. Admin::ProductsController) in an Admin:: module. Unfortunately, this seems to be causing issues with basic functionality, such as form_for(@product) producing

undefined method `products_path' for #<#<Class:0x103dc4110>:0x103dc1618>

This is presumably because the routes.rb entries

namespace :admin do
    resources :products
end

produce

admin_products GET    /admin/products(.:format)   {:action=>"index", :controller=>"admin/products"}

with t开发者_开发知识库he "admin_" prefix, yet form_for(@product) doesn't seem to understand that.

I would like to just declare to Rails that the controller to edit Products is Admin::ProductsController, but that link between the two seems to be missing. I've tried:

  • Tweaking the parameters on the routes.rb declaration (but being a total Rails n00b, it's entirely possible I missed the right way to do it)
  • Looking for some form of declaration in the model class to change the way form_for deduces the _path helper
  • Moving the models to the Admin namespace; however, this is not only undesirable conceptually because the model objects are used elsewhere outside of Admin:: and don't have any particular semantic tie to administration (just the editing etc. in the controller), but it also caused problems with testing and fixtures in particular that I was unable to solve.

What's the most Rails-y, modern way to structure this kind of system, and how can I remedy the issue? I feel like I've been fighting the framework, so if there's an overall better way to do it I'm open to suggestions, but even my trying to skirt the problem entirely by putting the models in Admin:: just opened up new issues…


This should do what you want: form_for [:admin, @product]


Try form_for @product, :url => admin_product_path(@product)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号