CARVIEW |
Every repository with this icon (

Every repository with this icon (

Run the following if you haven't already:
gem sources -a https://gems.github.com
Install the gem(s):
sudo gem install tred-fancyroutes
Description: | Removing the cruft, bringing the HTTP method to the forefront and sporting a neat DRYing block syntax--FancyRoutes is a layer on top of the Rails routing system which provides an elegant API for mapping requests to your application's controllers and actions. |
Homepage: | https://tred.github.com/fancyroutes |
Clone URL: |
git://github.com/tred/fancyroutes.git
Give this clone URL to anyone.
git clone git://github.com/tred/fancyroutes.git
|

name | age | message | |
---|---|---|---|
![]() |
.gitignore | Tue Mar 03 05:11:35 -0800 2009 | Minor changes for 0.9.0 release. [Chris Lloyd] |
![]() |
LICENSE | Tue Mar 03 05:08:45 -0800 2009 | Gemifiy this bitch. [Chris Lloyd] |
![]() |
README.md | Fri Mar 06 23:43:56 -0800 2009 | Remove da codes! [toolmantim] |
![]() |
Rakefile | Sun Mar 08 18:10:57 -0700 2009 | Bump to 0.9.1. [Chris Lloyd] |
![]() |
fancyroutes.gemspec | Sun Mar 08 18:10:57 -0700 2009 | Bump to 0.9.1. [Chris Lloyd] |
![]() |
init.rb | Tue Mar 03 05:08:45 -0800 2009 | Gemifiy this bitch. [Chris Lloyd] |
![]() |
lib/ | Wed Mar 04 20:20:31 -0800 2009 | Syntax for root path: root >> controller > acti... [Chris Lloyd] |
![]() |
spec/ | Sat Mar 07 19:59:48 -0800 2009 | Consistantized(!) the spec action and controlle... [toolmantim] |
FancyRoutes
Removing the cruft, bringing the HTTP method to the forefront and sporting a neat DRYing block syntax--FancyRoutes is a layer on top of the Rails routing system which provides an elegant API for mapping requests to your application's controllers and actions.
Take for example the following routes from one of Myles' applications:
map.connect '/orders', :controller => 'orders', :action => 'index, :conditions => { :method => :get }
map.connect '/:slug/order', :controller => 'orders', :action => :show, :conditions => { :method => :get }
map.connect '/:slug/order', :controller => 'orders', :action => :create, :conditions => { :method => :post }
map.connect 'item_images/:image', :controller => 'item_images', :action => 'show', :conditions => { :method => :get }
Converted to fancyroutes these now look like:
get / 'orders' >> :orders > :index
with route / :slug / 'order' >> :orders do
get > :show
put > :update
end
get {'item_images' => :controller} / :image > :show
So fancy!
Where'd all the parentheses go?
We use three of Ruby's operators to define paths:
/
separates the segments>>
indicates the controller>
indicates the action
Segments of the path can be strings or symbols. Symbols, such as :image
, define parameters and strings, such as 'order'
, define static segments.
Controller names in the path
Quite often the controller name will already be in the path itself.
map.connect '/playground/:action', :controller => 'playground'
Don't repeat yourself! Provide a hash instead:
get / {'playground' => :controller} / :action
Named routes
Simply suffix the get/post/put/delete with the name:
page.get / '*tree' >> :pages > :show
Now you can generate a path with the route:
page_path(["help", "where-is-the-any-key"])
Nesting routes
Use with route
to DRY up similar routes. For example:
get / :slug / 'order' >> :orders > :show
put / :slug / 'order' >> :orders > :update
can be rewritten as:
with route / :slug >> :orders do
get / 'order' > :show
put / 'order' > :update
end
or even better:
with route / :slug / 'order' >> :orders do
get > :show
put > :update
end
The root route
A standard root route looks something like this:
map.root :controller => 'homepage', :action => 'index'
The fancier version is:
root >> :homepage > :index
Installing
Install the gem:
sudo gem install tred-fancyroutes
add the dependency in your environment.rb:
config.gem 'tred-fancyroutes'
and then use the FancyRoutes method in your routes.rb:
ActionController::Routing::Routes.draw do |map|
FancyRoutes(map) do
# the
# fanciest
# routes
# you
# ever
# did
# see
end
end
Contributors
- Myles Byrne
- Carl Woodward
- Tim Lucas
- Chris Lloyd
- Michael Koukoullis
- Lincoln Stolli
- Dave Newman
License (MIT)
Copyright (c) 2008-09 The TRED Team
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.