Misc. Routing
There are a few other misc. features in the routing engine.
Mack::Routes.build do |r| r.home_page '/', :controller => :default, :action => :index, :host => 'www.example.com' r.admin_home = '/', :controller => :admin, :action => :index, :port => 8080, :host => 'admin.example.com' r.users_home_page '/', :controller => :users, :action => :show_by_username, :host => ':username.example.com' r.resource :users r.login '/login', :controller => :sessions, :action => :new, :scheme => 'https' end
Let's take these routes line by line and see what features we're looking at.
r.home_page '/', :controller => :default, :action => :index, :host => 'www.example.com'
Here we're saying that any request that comes that matches '/' AND the host matches 'www.example.com' send it to DefaultController#index.
home_page_url # => '<current_scheme or http>://www.example.com<:port unless 80 or 443>/'
Next...
r.admin_home = '/', :controller => :admin, :action => :index, :port => 8080, :host => 'admin.example.com'
Here we're saying that any request that comes that matches '/' AND the host matches 'admin.example.com' AND the port is 8080 send it to AdminController#index.
admin_home_url # => '<current_scheme or http>://admin.example.com:8080/'
Next...
r.users_home_page '/', :controller => :users, :action => :show_by_username, :host => ':username.example.com'
Here we're saying that any request that comes that matches '/' AND the host matches '*.example.com' send it to UsersController#show_by_username. We're also going to set params[:username] = the value of '*', so 'http://markbates.example.com' would set params[:username] = 'markbates'.
users_home_page_url(:username => 'markbates') # => '<current_scheme or http>://markbates.example.com<:port unless 80 or 443>/'
Next...
r.resource :users
The standard resource routing applies here. It should be noted that because no host, port, or scheme is specified that the request will match on ANY host, port, or scheme.
Next...
r.login '/login', :controller => :sessions, :action => :new, :scheme => 'https'
Here we're saying that any request that comes that matches '/login' AND the scheme matches 'https' send it to SessionsController#new.
login_url # => 'https://<current_host><:port unless 80 or 443>/login'