I''m working on a small Rails application which needs to behave differently based on the domain name provided to it. (It''s a special-purpose search engine which needs to have a different look and use a different database for each virtual host.) The problem we''ve hit is we can''t find a clean way to determine the hostname from inside the controller class. I wasn''t able to find anything in the documentations I found. Right now we''re running a Regex on @url.rewrite.to_s, which feels kinda dirty. Is there a clean way to determine the hostname? Thanks for any answers or (even better) pointers to documentation along these lines. Everyone in the office is kind of a Rails-n00b. Pete
How about looking at the env hash provided in the @request object. e.g. @request.env["REQUEST_URI"] http://rails.rubyonrails.com/classes/ActionController/Base.html Or this if you want to do something like what BaseCamp does: http://wiki.rubyonrails.com/rails/show/HowToUseSubdomainsAsAccountKeys On Tue, 2005-03-22 at 18:46 -0800, Pete Elmore wrote:> I''m working on a small Rails application which needs to behave > differently based on the domain name provided to it. (It''s a > special-purpose search engine which needs to have a different look and > use a different database for each virtual host.) > > The problem we''ve hit is we can''t find a clean way to determine the > hostname from inside the controller class. I wasn''t able to find > anything in the documentations I found. Right now we''re running a Regex > on @url.rewrite.to_s, which feels kinda dirty. Is there a clean way to > determine the hostname? > > Thanks for any answers or (even better) pointers to documentation along > these lines. Everyone in the office is kind of a Rails-n00b. > > Pete > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Pete Elmore wrote:> The problem we''ve hit is we can''t find a clean way to determine the > hostname from inside the controller class. I wasn''t able to find > anything in the documentations I found. Right now we''re running a Regex > on @url.rewrite.to_s, which feels kinda dirty. Is there a clean way to > determine the hostname?@request.host gives you the hostname. @request.domain and @request.subdomains split it into convenient parts. Use a before_filter in ApplicationController to do domain-specific setup. Say you want to load site settings from the database: class ApplicationController < ActionController::Base before_filter :load_site_for_domain protected attr_reader :site helper_method :site def load_site_for_domain @site = Site.find_by_domain(@request.domain) end end jeremy