Screencast: Subdomains in Rails 3

 

Download:

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

 

Resourcen:

 

Quellcode:

# 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"
<!-- 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) %>

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>