Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails route to only index

This might be a simple routing question in Rails but I have searched around and received answers for Rails 2 rather than Rails 3.

I generated a scaffold and the

resources :users  

which includes new, edit, show are routed together with the index.

I only want to route to the index and remove the new, edit, show etc. I have already removed the html.erb files but they are still being routed.

Any advice on what I should do to remove the other routes would be appreciated.

like image 216
Butter Beer Avatar asked Dec 18 '12 16:12

Butter Beer


People also ask

What are RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.

What is only in Rails?

The :only option tells Rails to create only the specified routes: resources :photos, :only => [:index, :show] Follow this answer to receive notifications.

How do you get a Rails route?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.


2 Answers

Use the only option:

resources :users, only: [:index] 

Reference

like image 104
Rodrigo Avatar answered Sep 28 '22 04:09

Rodrigo


See Chapter 4.6 of the Rails Routing Guide.

By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the :only and :except options to fine-tune this behavior. The :only option tells Rails to create only the specified routes:

resources :photos, :only => [:index, :show] 
like image 45
Mike Campbell Avatar answered Sep 28 '22 04:09

Mike Campbell