Support for multiple domains / subdomains in Rails

I have a Rails application with a similar setting on Tumblr, that is, you can: (1) Hosting subdomain (your-username.myapp.com) (2) Domain hosting (your-username.com)

Both will be redirected to a personalized website for this user created with my application.

How to do it in Rails? I was able to get (1) work with subdomain-fu, but I'm not sure how to get (2) work. Any pointers (plugins, gems, tutorials), etc. Would be very helpful, I can not find anyone.

Thank!

+2
ruby-on-rails
Feb 22 '10 at 5:00
source share
2 answers

The principle for domains coincides with the subdomain - find the domain, map to the account.

Details will depend on how your hosting handles DNS.

I am currently using Heroku and its wildcard service. In this case, the domain is mapped by cname to the subdomain hosted in my Heroku application. From here I can develop the appropriate account and details.

+3
Feb 22 '10 at 5:29
source share
— -

EDIT: I found a much simpler way: http://www.arctickiwi.com/blog/7-host-and-domain-based-routing-in-ruby-on-rails

Not quite the answer, but this is the best I can give. Perhaps this will help too.

Ideally, this blog post from transfs.com and subdomain-fu should do the trick. However, I am trying to implement it, and they do not seem to play well together.

Basically, if I don't turn on the intiializer, the subdomain route works fine. If I turn on the initializer, the subdomain route breaks (everything gets caught by map.root). I have a feeling that he is building a condition string in the initializer. If you can understand how it breaks, then you will have a working application.

My initializer:

 module ActionController module Routing class RouteSet def extract_request_environment(request) env = { :method => request.method } env[:domain] = request.domain if request.domain env[:host] = request.host if request.host 
env end end class Route alias_method :old_recognition_conditions, :recognition_conditions def recognition_conditions result = old_recognition_conditions [:host, :domain].each do |key| if conditions[key] operator = "===" if conditions[key].is_a?(Regexp) operator = "=~" end result << "conditions[:#{key.to_s}] #{operator} env[:#{key.to_s}]" end end result end end# end class Route end end

My routes (for development only). You will see my local development domain, stiltify.dev. Sorry, I tried to make it look good here, but I could not make the code block look beautiful. Instead, I put on a paste: http://pastie.org/940619 .

The comment section in the screencast of Ryan Bates was very useful and made me figure out the subdomain => false and other errors they entered. However, the problem persists!

+2
Apr 30 '10 at 17:50
source share



All Articles