RailsCasts Pro episodes are now free!
Learn more or hide this
Resources
Note: If you are using Rails 3.1 or later, be certain to check out revised episode 123.
constraints(Subdomain) do match '/' => 'blogs#show' end
class Subdomain def self.matches?(request) request.subdomain.present? && request.subdomain != "www" end end
module UrlHelper def with_subdomain(subdomain) subdomain = (subdomain || "") subdomain += "." unless subdomain.empty? [subdomain, request.domain, request.port_string].join end def url_for(options = nil) if options.kind_of?(Hash) && options.has_key?(:subdomain) options[:host] = with_subdomain(options.delete(:subdomain)) end super end end
include UrlHelper
# requires Rails 3.0 RC or head Rails.application.config.session_store :cookie_store, :key => '_blogs_session', :domain => :all # change top level domain size request.domain(2) request.subdomain(2) Rails.application.config.session_store :cookie_store, :key => '_blogs_session', :domain => "example.co.uk"
<%= link_to blog.name, root_url(:subdomain => blog.subdomain) %>
<%= link_to "All Blogs", root_url(:subdomain => false) %>