Using resources with custom controller names

℡╲_俬逩灬. 提交于 2020-01-12 13:46:33

问题


I'm using nested resources, however i come across controller names that should be more descriptive.

For instance i have a controller ProductsController and ImagesController

resources :products do
  resources :images
end

This works fine, but later i might need to use the ImageController for other than products images, therefore it should be named ProductsImagesController.

But how can i specify the controller name on resources() without falling back to something ugly like:

match 'products/images' => 'products_images#index'
match 'products/images/new' => 'products_images#new'

回答1:


resources :products do
  resources :images, :controller => "products_images"
end



回答2:


Coming from a Zend Framework background, I think you are looking for a modular structure. Rails seems to offer this, called 'namespacing':

namespace :admin do
  resources :posts, :comments
end

That creates routes to Admin::PostsController and Admin::CommentsController. In your case, you would have Products::ImagesController.

http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

I found out from this other accepted answer: zend modules like in rails



来源:https://stackoverflow.com/questions/12553870/using-resources-with-custom-controller-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!