Just added to Edge Rails is the ability to exclude and include the default generated routes in your mapping configuration. Previously, map.resources :articles
would generate routes to all seven default actions on the ArticlesController
(index, create, new, edit, show, update, destroy). You can now tell your routes configuration to only generate a subset of those actions, or to exclude a subset of those actions:
map.resources :articles, :only => :index
# Generate all but the destroy route of articles
map.resources :articles, :except => :destroy
# Only generate the non-modifying routes of articles
map.resources :articles, :only => [:index, :show]
Note that you can use the :all
and :none
values to denote all or none of the default routes.
map.resources :articles, :except => :all, :member => { :approve => :put }
# Same
map.resources :articles, :only => :none, :member => { :approve => :put }
You should also note that these options will be inherited by nested resources that don’t override them. For instance, in this example, comments would only have the :index
and :show
routes exposed:
# Because comments are nested within articles, they too will only
# have the index and show routes generated.
map.resources :articles, :only => [:index, :show] do |article|
article.resources :comments
end
Specifying either an :except
or :only
option in a nested resource will override its parent resource’s options. E.g. this routing will result in comments
having all actions but :show
routed:
map.resources :articles, :only => [:index, :show] do |article|
# My :except option overrides my parent resources :only
article.resources :comments, :except => :show
end
The motivation behind this feature is that complex routing consumes a lot of memory. So eliminating unnecessary and unused routes can significantly reduce your memory consumption. The holy grail of this, however, will be when you can specify which routes should be formatted (json
, xml
etc…) as this is essentially doubling the number of default routes when very rarely does every call need to be API-accessible.
http://ryandaigle.com/articles/2008/11/13/what-s-new-in-edge-rails-except-and-only-routing-options
No More Formatted Routes
Parallel to the addition of :except and :only options to Rails’ routing comes the removal of all those formatted_xxx
named routes. Turns out these routes ate up a lot of memory and served minimal purpose.
So with this routes.rb
definition:
ActionController::Routing::Routes.draw do |map|
map.resources :articles
end
you’ll no longer have any of the formatted_xxx
url helpers available. However, you will be able to get the same functionality by passing in a :format
option to the base view helper methods:
# Old => New
formatted_article_path(article, :xml) =>
article_path(article, :format => :xml)
formatted_new_article_path(:json) =>
new_article_path(:format => :json)
# etc...
A minimal change that will have a big impact on the memory consumption of each of your Rails processes – especially if you’ve got a complex app with lots of routes.
If you’re on Edge Rails you’re going to start seeing deprecation warnings where you reference these formatted url helpers, so you’ve got some warning before you have to make the switch.
http://ryandaigle.com/articles/2008/11/27/what-s-new-in-edge-rails-no-more-formatted-routes
No comments:
Post a Comment