The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: :except and :only Routing Options

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: :except and :only Routing Options Posted: Nov 13, 2008 7:02 AM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

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:

1
2
3
4
5
6
7
8
# Only generate the :index route of articles
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.

1
2
3
4
5
# Don't generate any default article routes, just the approve route
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:

1
2
3
4
5
# 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

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:

1
2
3
4
5
6
7
8
# Since :only takes precedence, even in inherited options,
# comments will have both index and show
map.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 => :show
end

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.

tags: ruby, rubyonrails

Read: What's New in Edge Rails: :except and :only Routing Options

Topic: DIY Maps and Linux Firefox/Flash Quirks Previous Topic   Next Topic Topic: Ruby Conf 2008 Last Day

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use