rails3已经内置subdomain

  1. Custom Subdomains in Rails 3

     

    Rails 3 supports subdomains out of the box, which is great. But did you know that the constraints in the Router can actually match patterns too? This means instead of hardcoding each subdomain into your routes you can allow your customers to decide their own subdomains.

    However, we have to be careful with pattern matching on the subdomain. There are obvious subdomains we don’t want to match. Like ‘www’, ”, nil, and others that we may reserve. In this case using a pattern match might not be best.

    Thankfully the Rails 3 Router constraints can also take objects. As long as the object responds to Object.matches?. The request is passed to the method and you can act on it in any way. The following is the solution that I’ve found works for me.

    I created a ‘lib/sub_domain.rb’ with the following code:

    # lib/sub_domain.rb
    class SubDomain
      def self.matches?(request)
        case request.subdomain
        when 'www', '', nil
          false
        else
          true
        end
      end
    end
    

    In my routes.rb file I can now wrap all routes I want under a custom subdomain

    # config/routes.rb
    TestApp::Application.routes.draw do |map|
      constraints(SubDomain) do
        root :to => "customers#index"
      end
      
      root :to => "home#index"
    end
    

    Finally, I create a SubDomainController from which all controllers under the subdomain constraint can inherit from

    # app/controllers/sub_domain_controller.rb
    class SubDomainController < ApplicationController
      before_filter :get_customer_from_subdomain
      
      private
      
      def get_customer_from_subdomain
        @customer = Customer.find_by_subdomain!(request.subdomain)
      end
    end
    
    # app/controllers/customers_controller.rb
    class CustomersController < SubDomainController
      def index
        ...
      end
    end
    

    Having the SubDomainController is a nice way for me to encapsulate behavior that I want every subdomain to have. One such idea would be customer specific layouts. (or themes)

    Check out Phil McClure’s post on localhost subdomains if you want to use this functionality in your development and test environments.

    Update

    To link to your dynamic subdomains you can completely overwrite the :host option in the url helper:

    root_url(nil, {:host => "subdomain.somedomain.com"})
    

    This is not ideal. It constrains us to this particular domain. What we need is to be able to pass a :subdomain option to the url helper. (btw, you need to use the url helpers for linking to subdomains and not the path helpers)

    So I quickly wrote up this code. Just add it to your ApplicationController and it will be available to your entire app:

    class ApplicationController < ActionController::Base
      ...
      private
      
      # To write subdomains on the url helpers:
      # root_url(nil, {:subdomain => "subdomain"})
      def url_for(options = nil)
        case options
        when Hash
          if subdomain = options.delete(:subdomain)
            if request.subdomain.empty?
              options[:host] = "#{subdomain}.#{request.host_with_port}"
            else
              options[:host] = request.host_with_port.sub(request.subdomain, subdomain)
            end
          end
        end
        super
      end
    end
    

    So now you can do:

    root_url(nil, {:subdomain => "subdomain"})
    

Notes

 

  1. jackhq reblogged this from bcardarella
  2. bcardarella posted this
DISQUS ...
  • 4 people liked this.
Community

Glad you liked it. Would you like to share?

Facebook

Twitter

Sharing this page ...

Thanks! Close

Add New Comment

Showing 1 comments

Sort by Popular nowBest ratingNewest firstOldest first   Subscribe by email   Subscribe by RSS
  • A tip for people trying to use the url_for method...

    Adding that method to the ApplicationController will only allow subdomains to be used in your controllers. If you want to do the same in your views then you'll need to add:

    helper_method :url_for

    to your ApplicationController as well.
View the discussion thread.

 

 

posted on 2010-12-16 11:36  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/12/16/1907726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值