Subdomains and Rails 2.1

Rails framework has many plugins for handling subdomains. One of the recommended plugins to achieve the same is Subdomain-fu.

Subdomain-fu uses Rails’s URL Writing mechanisms to provide an easy and seamless way to link and otherwise understand cross-subdomain routing.

Before attempting to know, I suggest to read a write up by Jamis Buck on understanding how rails routing works here.

Palani, my peer in Thoughtworks and I worked on implementing subdomains in our current project.

In our opinion, subdomain-fu works well as advertised, but, we had to patch up lib/subdomain_fu/routing_extensions.rb and changed the recognition_conditions_with_subdomain routine.

By default, it was taking conditions hash and we changed it to use requirements hash. The effect is that we can now pass :subdomain symbol as another parameter in map keyword of routes.rb.

The recognition_conditions_with_subdomain routine becomes like this:

def recognition_conditions_with_subdomain
result = recognition_conditions_without_subdomain
result << "requirements[:subdomain] === env[:subdomain]" if requirements[:subdomain] && requirements[:subdomain] != true && requirements[:subdomain] != false
result << "SubdomainFu.has_subdomain?(env[:subdomain])" if requirements[:subdomain] == true
result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if requirements[:subdomain] == false
result
end

Routing works amazing!

Stats

  • 142,057 Hits and counting...

Archives

Follow

Get every new post delivered to your Inbox.