43

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.

2 Answers 2

87

Use the only option:

resources :users, only: [:index]

Reference

1
  • 2
    Or if you want to add only one route to resource, you may skip brackets and write like this resources :users, only: :index
    – dshevc93
    Commented Nov 9, 2016 at 18:30
14

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]
2
  • 2
    if 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 Commented Jul 22, 2014 at 7:07
  • @waqarmirza you can add constrains to the show route to limit the /:id to be a integer
    – fengd
    Commented Jun 15, 2016 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.