Alexander Popov
Alexander Popov

Reputation: 24875

How to name a route on a resource?

If I have a controller Home with a single action index, I know I can write:

get 'home' => 'home#index"

and I will automatically have the named route home_path.

But if I define it like so:

resources :home, only: :index

I get the route home_index_path. Why is that and how can I create the named route home_path, if I use the resources convention.

Upvotes: 1

Views: 1174

Answers (2)

Nitin
Nitin

Reputation: 7366

It is possible to use singular name controller with rails routing. try use :

resource :home, :controller => 'home'

Upvotes: 1

pdobb
pdobb

Reputation: 18037

Rails defines a standard naming convention for all resources in the routes file. See the Rails Guide on Resource Routing.

You can also run bundle exec rake routes to see the names for the routes you've defined in your application.

In the case of your example, resources :home, only: :index, the named route for the "homes#index" action would be homes_path or homes_url.

UPDATE:

You may prefer resource :home, only: :show which would have a named route of home_path or home_url. Also the path to access the home resource (singular) would be /home whereas the the home resources (plural) index would be /homes.

Upvotes: 1

Related Questions