Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using resources with custom controller names

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'
like image 416
CodeOverload Avatar asked Sep 23 '12 16:09

CodeOverload


People also ask

How do I name a resource route in laravel?

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.

What is the difference between resource and resources?

Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.

What are resources in Rails routes?

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.

What does rake routes do?

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.


2 Answers

resources :products do
  resources :images, :controller => "products_images"
end
like image 103
cdesrosiers Avatar answered Sep 30 '22 16:09

cdesrosiers


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

like image 35
tuespetre Avatar answered Sep 30 '22 16:09

tuespetre