router_bits
router_bits
extends Rails routing conditions beyond the request’s method. Conditions are created for commonly used attributes of ActionController::AbstractRequest
such as: host
, domain
, subdomains
and xhr?
.
Example
Conditionally route requests based on domain name:
map. :controller => 'example', :conditions => {:domain => 'example.com'} do |map|
map.root
end
map. :controller => 'sample', :conditions => {:domain => 'sample.com'} do |map|
map.root
end
Differentiate Ajax and non-Ajax requests:
map. :controller => 'users' do |map|
map.profile 'user/:id', :action => 'profile', :conditions => {:xhr => false}
map.mini_profile 'user/:id', :action => 'mini_profile', :conditions => {:xhr => true}
end
Tips
-
Only extends routing conditions; not used in URL generation
-
Although often equivalent,
host
anddomain
represent two separate pieces of information. For a request to www.sub.domain.com the value forhost
is"www.sub.domain.com"
while the value fordomain
is"domain.com"
. -
The value for
subdomains
always returns an array. In the above case,subdomains
would be["www", "sub"]
.