resource routes to be mapped to a custom path in rails 4
Asked Answered
A

2

17

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

Axes answered 18/2, 2015 at 10:38 Comment(0)
B
23

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.

Brodeur answered 18/2, 2015 at 12:37 Comment(0)
U
13

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

Underwear answered 18/2, 2015 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.