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'
x | Laravel 5. x). There are two ways you can modify the route names generated by a resource controller: Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.
Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.
Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.
rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.
resources :products do
resources :images, :controller => "products_images"
end
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With