0

I want to be able to get the host in the REST request directly from the routes.rb ( my application can be called from 2 different addresses )

I know that the object request is available from ActionController, but that is accessible only starting from application_controller.rb

Thank you very nuch

user3442206
  • 577
  • 1
  • 6
  • 22

1 Answers1

1

In one of my apps i route differently depending on which host/domain we're on ("cmw" or the normal site). This is how i set it up:

#in config/routes.rb
cmw = { :host => CMW_HOST_REGEX }  #CMW_HOST_REGEX set in a constants file

#cmw routes
map.cmw_home '/',  :controller => "cmw/home", :conditions => cmw
map.connect '',    :controller => "cmw/home", :conditions => cmw
map.resources :students, :conditions => cmw

#regular section - don't have the condition 
map.home '/',  :controller => "home"
map.connect '',    :controller => "home"
map.resources :teachers
#etc

The key thing here is that you can pass a value :host => <regex> to the :conditions option on each route, which will apply that regex to the current hostname.

EDIT - this is rails 2 (and maybe 3) syntax btw, but you should get the idea and be able to adapt it to whatever version of rails you're using.

EDIT 2 - note that because the cmw routes are more specific, with the extra condition, they all need to be listed BEFORE the "normal" routes, since routes are matched in order from top to bottom of the file.

Max Williams
  • 32,435
  • 31
  • 130
  • 197