This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: :except and :only Routing Options
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
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:
12345678
# Only generate the :index route of articlesmap.resources :articles, :only => :index# Generate all but the destroy route of articlesmap.resources :articles, :except => :destroy# Only generate the non-modifying routes of articlesmap.resources :articles, :only => [:index, :show]
Note that you can use the :all and :none values to denote all or none of the default routes.
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:
12345
# 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 :commentsend
Keep this inheritance behavior in mind when nesting routes as, in the current implementation, conflicting options may yield unexpected results. Currently the :only option takes precedence if you have conflicting :only and :except sets, including inherited values. E.g. this routing will result in comments having both :index and :show routed:
12345678
# Since :only takes precedence, even in inherited options,# comments will have both index and showmap.resources :articles, :only => [:index, :show] do |article|# I will still have :index and :show because I've inherited# article's routing and its :only overrides my :except article.resources :comments, :except => :showend
So be careful of conflicting options. If you can’t void them, it will help to redefine both :only and :except so there are no unexpected routes.