12

I have a route like:

resources :products

Now I have all my code is in place but just need to change the paths from /products/:action to /items/:action

I have already skimmed through the rails docs but could not figure this out. It looks very basic and should be easy, but I just cant put my finger on it.

The url I used was: http://guides.rubyonrails.org/routing.html#path-and-url-helpers

whizcreed
  • 2,509
  • 2
  • 19
  • 33

2 Answers2

20

You can write your route like this:

resources :products, path: 'items'

This will generate /items routes with product_* named helpers, using ProductsController. Have a look at this part of the Routing Guides.

dgilperez
  • 9,981
  • 8
  • 60
  • 89
12

There are several ways you can accomplish this. One is to simply name your resource items and specify the controller with the :controller option.

resources :items, controller: 'products'

This will recognize incoming paths beginning with /items but route to the ProductsController. It will also generate route helpers based on the resource name (e.g. items_path and new_item_path).

Another way is to use the :path option when specifying the resource as pointed out by @dgiperez.

resources :products, path: 'items'

This will also route paths beginning with /items to the ProductsController but since the route helpers are based on the resource name, they would be based on products (e.g. products_path and new_product_path)

Reference

Bart Jedrocha
  • 10,951
  • 4
  • 41
  • 52