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.with_options :controller => 'example', :conditions => {:domain => 'example.com'} do |map|
  map.root
end

map.with_options :controller => 'sample', :conditions => {:domain => 'sample.com'} do |map|
  map.root
end

Differentiate Ajax and non-Ajax requests:

map.with_options :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 and domain represent two separate pieces of information. For a request to www.sub.domain.com the value for host is "www.sub.domain.com" while the value for domain is "domain.com".

  • The value for subdomains always returns an array. In the above case, subdomains would be ["www", "sub"].