Fork me on GitHub

Restrict auto-generated routes.

19 Aug 2011

Andy Wang

For instance, you've got a comments_controller, and your application only needs its "create" and "destroy" actions. How will you define your routes?

Normally, people will do:

resources :comments

This will generate seven routes for you by Rails' default. Such as:

comments      GET       /comments(.:format)
              POST      /comments(.:format)
new_comment   GET       /comments/new(.:format)
edit_comment  GET       /comments/:id/edit(.:format)
comment       GET       /comments/:id(.:format)
              PUT       /comments/:id(.:format)
              DELETE    /comments/:id(.:format)


resources :comments, :only => [:create, :destroy]

New generated routes are only two:

              POST      /comments(.:format)
              DELETE    /comments/:id(.:format)

Comparing to :only, the :except option also works well to EXCEPT the included routes.

If your application defines many reoutes, the above restricting on routes will delete many useless routes, that means cutting down on memory use for you and speeding up the routing process.

Simple but a good habit! Enjoy it.