Rails route to only index
Solution 1
Use the only
option:
resources :users, only: [:index]
Solution 2
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]
Butter Beer
Updated on June 28, 2020Comments
-
Butter Beer almost 4 years
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.
-
waqar mirza almost 10 yearsif I add
resources :photos, :only => [:index, :show]
in routes, and hit '/photos/new' it routes to photos#show with id='show'. How do I route :new/:edit routes to 404? Please help -
fengd almost 8 years@waqarmirza you can add constrains to the
show
route to limit the/:id
to be a integer -
dshevc93 over 7 yearsOr if you want to add only one route to resource, you may skip brackets and write like this
resources :users, only: :index