Nathaniel S. H. Brown
2005-Dec-30 23:52 UTC
Unable access which domain in my routes.rb to map custom route tables per domain
I have tried all night trying to find a way to get the host name which is currently being used within my routes.rb file to do some case/when switching for specific domains such as this cgi = ActionController::CgiRequest.new(CGI.new) case cgi.domain(1) when /www/ map.connect '''', :controller => ''public'', :action => ''index'' when /internal/ map.connect '''', :controller => ''admin'', :action => ''index'' end And have had no success. The domain(1) is empty in this case. I have tried a similar approach in one of my views, using the @request.domain(1) and that works PERFECT. Yet I have found no way to access the same @request.domain(1) within my routes.rb as the @request object has not been loaded yet. One thought was to use the ENV[''HTTP_HOST''] variable. But this appears to be empty with my lighttpd/apache2 setup. - Nathaniel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Nathaniel S. H. Brown http://nshb.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Duane Johnson
2006-Jan-02 17:04 UTC
Re: Unable access which domain in my routes.rb to map custom route tables per domain
On Dec 30, 2005, at 4:52 PM, Nathaniel S. H. Brown wrote:> I have tried all night trying to find a way to get the host name > which is > currently being used within my routes.rb file to do some case/when > switching > for specific domains such as this > > cgi = ActionController::CgiRequest.new(CGI.new) > case cgi.domain(1) > when /www/ > map.connect '''', :controller => ''public'', :action => > ''index'' > when /internal/ > map.connect '''', :controller => ''admin'', :action => > ''index'' > end > > And have had no success. The domain(1) is empty in this case. > > I have tried a similar approach in one of my views, using the > @request.domain(1) and that works PERFECT. Yet I have found no way > to access > the same @request.domain(1) within my routes.rb as the @request > object has > not been loaded yet. > > One thought was to use the ENV[''HTTP_HOST''] variable. But this > appears to be > empty with my lighttpd/apache2 setup. > > - NathanielNathaniel, This sounds like a topic for discussion outside of the core list (unless you''re looking for advice on how to implement this feature into the Rails code base or would like some kind of peer review for your idea or code). I''ll send a message to the rails list with my thoughts on this situation. Duane Johnson (canadaduane) http://blog.inquirylabs.com/
Duane Johnson
2006-Jan-02 17:15 UTC
[Rails] Re: [Rails-core] Unable access which domain in my routes.rb to map custom route tables per domain
On Dec 30, 2005, at 4:52 PM, Nathaniel S. H. Brown wrote:> I have tried all night trying to find a way to get the host name > which is > currently being used within my routes.rb file to do some case/when > switching > for specific domains such as this > > cgi = ActionController::CgiRequest.new(CGI.new) > case cgi.domain(1) > when /www/ > map.connect '''', :controller => ''public'', :action => > ''index'' > when /internal/ > map.connect '''', :controller => ''admin'', :action => > ''index'' > end > > And have had no success. The domain(1) is empty in this case. > > I have tried a similar approach in one of my views, using the > @request.domain(1) and that works PERFECT. Yet I have found no way > to access > the same @request.domain(1) within my routes.rb as the @request > object has > not been loaded yet. > > One thought was to use the ENV[''HTTP_HOST''] variable. But this > appears to be > empty with my lighttpd/apache2 setup. > > - NathanielI''ve had a similar need to use the domain in my routes. Unfortunately, there is no easy solution. The reason your code isn''t working is because the routes system is actually a code generator that pre-generates Ruby code from your list of rules. Once generated, it is executed like a big ''if/elsif/else'' statement to see which route should be called. However, there is no host variable available within the generated code with which you might solve the problem above. In short, the routes system needs a serious overhaul to accept host- based conditions for routing. I''ve attempted a patch for this, but my implementation was a surface-only change and it turned out to be much, much more complicated than I had thought. As a result, I''ve turned to components as a temporary solution to the problem--I have a DispatcherController that is responsible for dispatching certain "routes" by rendering the appropriate controller and view as a component. My routes are configured like this: # Take care of all other routes with a custom dispatcher map.connect ''*path'', :controller => ''dispatcher'' And then in my DispatcherController: def index if params[:path].first == ''card'' elsif request.host_with_port == ''localhost:3000'' if request.path == ''/'' render_component \ :controller => ''welcome'' else render :text => "not found" end else # ... other code end end You might also be interested in my recent post, ''A Roundabout Way to Write Routes'' (http://inquirylabs.com/blog/?p=25). While it doesn''t solve this problem directly, it does add some flexibility for some routing situations. Duane Johnson (canadaduane) http://blog.inquirylabs.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060102/026b1f30/attachment.html