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.

Be Sociable, Share!
 

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) %>
Be Sociable, Share!

Eingestellt am 05.07.2010 um 14:34

Kategorie: Tutorials

Be Sociable, Share!

Kommentare für diesen Artikel wurden geschlossen.

Suchen auf rubyonrails.de

Aktuelle Rails Version + Abhängigkeiten:

Gem rails-3.0.0
actionmailer (= 3.0.0, runtime)
actionpack (= 3.0.0, runtime)
activerecord (= 3.0.0, runtime)
activeresource (= 3.0.0, runtime)
activesupport (= 3.0.0, runtime)
bundler (~> 1.0.0, runtime)
railties (= 3.0.0, runtime)

Rails auf Rubyforge
Rails auf Github