Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
# A simple route match, sends "/contact" to Info#contact # (i.e. the 'contact' method inside the 'Info' controller) r.match("/contact"). to(:controller => "info", :action => "contact") # Use placeholders (e.g. :book_id) in the match, and they will be passed along to params r.match("/books/:book_id/:action"). to(:controller => "books") # Use placeholders in the "to" results for more complicated routing, e.g. for modules r.match("/admin/:module/:controller/:action"). to(:controller => ":module/:controller") r.match("/admin/:module/:controller/:action/:id"). to(:controller => ":module/:controller") # Use a block to factor out repetitive parts r.match("/accounts") do |a| # The following will match "/accounts/overview" and route to Accounts#overview a.match("/overview"). to(:controller => "accounts", :action => "overview") a.match("/:id/:action"). to(:controller => "accounts") a.match("/:id/:action.:format"). to(:controller => "accounts") end # Use a regular expression as the path matcher. Note that you must specify the # ^ (beginning of line) and $ (end of line) boundaries if you desire them. r.match(%r{^/movies/:id/movie-[a-z][a-zA-Z\-]+$}). to(:controller => "movies", :action => "search_engine_optimizer") # Use square-bracket notation to replace param results with captures from the path r.match(%r[^/movies/(\d+)-(\d+)-(\d+)$]). to(:controller => "movies", :movie_id => "[1][2][3]", :action => "show") # Use the second optional argument of 'match' to be more specific about the request; # in this case, only accept the POST method for the /movies/create action r.match("/movies/create", :method => "post"). to(:controller => "movies", :action => "create") # Use variables from the 'match' as results sent to the controller in the params hash, # e.g. :user_agent[1] will be replaced with either 'MSIE' or 'Gecko' in the following case: r.match(%r[^/movies/(.+)], :user_agent => /(MSIE|Gecko)/). to(:controller => "movies", :title => "[1]", :action => "show", :agent => ":user_agent[1]") # The 'match' method can also be called without the path string or regexp. # In this example, direct all insecure traffic to a Insecure#index r.match(:protocol => "http://"). to(:controller => "insecure", :action => "index") # Use anonymous placeholders in place of the ugly-looking pattern, /([^\/.,;?]+)/ r.match("/::/users/::"). to(:controller => "users", :action => "[2]", :id => "[1]") # Putting it all together, and adding the requirement that we use an "admin" prefix on the # domain (e.g. admin.mysite.com), do some interesting stuff: r.match(:domain => /^admin\b/) do |admin| admin.match(%r[/([A-Z]\w+)\+([A-Z]\w+)/::]). to(:controller => "admin/users", :action => ":path[3]", :first_name => ":path[1]", :last_name => ":path[2]") end.to(:controller => "admin/users", :action => "default") # Note that the last line above sends all traffic in the "admin" subdomain to the # Admin::Users#default action if no other route is matched. # Create a deferred route. In this case, the decision of whether or not the route # is a match is made via the .xhr? call. Note that it's ok to put the hash in a # conditional because if the "if" statement is false, ruby returns nil (i.e. no match). r.match(%r[^/deferred]).defer_to do |request, path_match| {:controller => "ajax", :action => "index"} if request.xhr? end # Use the matches from the path in a deferred route r.match("/deferred/:action").defer_to do |request, path_match| {:controller => "deferred", :action => path_match[1]} end
From the Design Piracy series on my blog: