Screencast: Subdomains in Rails 3

Seit Rails 3 ist es nun möglich Subdomains ohne zusätzliche Plugins zu benutzen bzw. einzusetzen. Ryan zeigt diese Woche wie.

 

Download:

Download(18.8 MB, 13:55)
Alternativer Download für iPod & Apple TV(18.5 MB, 13:55)

 

Resourcen:

 

Quellcode:

[ruby]
# routes.rb
constraints(Subdomain) do
match ‚/‘ => ‚blogs#show‘
end

# lib/subdomain.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end

# app/helpers/url_helper.rb
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

# application_controller.rb
include UrlHelper

# config/initializers/session_store.rb
# 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"
[/ruby]

[html]
<!– blogs/index.html.erb –>
<%= link_to blog.name, root_url(:subdomain => blog.subdomain) %>

<!– blogs/show.html.erb –>
<%= link_to "All Blogs", root_url(:subdomain => false) %>
[/html]