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,
hostanddomainrepresent two separate pieces of information. For a request to www.sub.domain.com the value forhostis"www.sub.domain.com"while the value fordomainis"domain.com". -
The value for
subdomainsalways returns an array. In the above case,subdomainswould be["www", "sub"].