Find your Rails subdomains

March 6, 2008

 
No Gravatar

Need to find the subdomain of an incoming request? Recently I needed to determine the subdomain of an incoming request to properly format some search queries. 

@subdomain =request.subdomains.first
  • srinathbtech
    Oops !! It's working great in my development/production environments..
    you can ignore my comment

    thanks,
    sri...
  • srinathbtech
    This is not working for me
    showing "nil" , I'm using rails 1.2.3 with mongrel server in dev mode.
    My code looks like this in production/development :

    in routes.rb

    map.connect '', :controller => "account", :action => "login", :conditions => {:domain => "tokyo.com"}

    in environment.rb

    module ActionController
    module Routing
    class RouteSet
    def extract_request_environment(request)
    { :method => request.method, :domain => request.domain}
    end
    end
    class Route
    alias_method :old_recognition_conditions, :recognition_conditions
    def recognition_conditions
    result = old_recognition_conditions
    result << "conditions[:domain] === env[:domain]" if conditions[:domain]
    result
    end # def
    end # class Route
    end # module Routing
    end # module ActionController


    i registered subdomain name as "sub.tokyo.com" for domain "tokyo.com" to provide different templates based on request url.

    in application.rb i used :

    def check_domain
    if request.domain == "tokyo.com"
    return true
    elsif request.subdomains.first == "sub"
    return false
    end
    end

    In development i configure /etc/hosts too
    127.0.0.1 : localhost
    127.0.0.1 : tokyo.com
    127.0.0.1 : sub.tokyo.com

    could you help me to fetch my sub domain
    is return nil when puts request.subdomains.first

    thanks
    sri..



    Oops !! It's working great in my development/production environments..
    you can ignore my comment

    thanks,
    sri...
blog comments powered by Disqus