2011-08-25 7 views
11

サブドメインを使用する私のレール3.1rc6アプリケーションにステージング環境と実稼働環境があります。私はこれらの環境で異なるドメイン名を購入して設定しました。なぜなら、デフォルトのsomething-something.herokuapp.comはサブドメインでうまく動作しないからです。ステージングとプロダクションを処理するsession_store.rbを設定しますか?

私は1つの環境のためにこれにsession_store.rbを設定すると、すべてが正常に動作します:

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' 

をしかし、私は環境固有のドメイン名を可能にするために、条件付きで追加するように見えることはできません。

私は動作しません

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' if Rails.env.staging? 
AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.myproductiondomain.com' if Rails.env.production? 

を試してみました。

答えて

6

あなたが使用することができます:domain => :allオプション。 1.

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => :all 

と異なる場合にも、ここでは、関連するRailsのコードは、そうでない場合

def handle_options(options) #:nodoc: 
    options[:path] ||= "/" 

    if options[:domain] == :all 
    # if there is a provided tld length then we use it otherwise default domain regexp 
    domain_regexp = options[:tld_length] ? /([^.]+\.?){#{options[:tld_length]}}$/ : DOMAIN_REGEXP 

    # if host is not ip and matches domain regexp 
    # (ip confirms to domain regexp so we explicitly check for ip) 
    options[:domain] = if (@host !~ /^[\d.]+$/) && (@host =~ domain_regexp) 
     ".#{$&}" 
    end 
    elsif options[:domain].is_a? Array 
    # if host matches one of the supplied domains without a dot in front of it 
    options[:domain] = options[:domain].find {|domain| @host.include? domain[/^\.?(.*)$/, 1] } 
    end 
end 

だ、:tld_lengthを提供することができ、あなたもパーにファイルの設定を上書きすることができるはずです環境基準。

+0

クラッキング、ありがとうございます。 – snowangel

+2

私はこの回答が私を助けたので、私はここに追加します。サブドメインのヘルパードメインlvh.meを使用して開発している場合は、DOMAIN_REGEXPを介してTLDと解釈され、example.comを模倣することはなく、代わりにexample.com.auのように動作しますtld_length of 1 [Github source](https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/cookies.rb#L103) – toxaq

16

以下の設定が私のために正常に動作されています:

設定/環境/ staging.rb

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' 

設定/環境/ production.rb

​​3210
+0

このオプションも私のために働いた - ありがとう。 – snowangel

+0

回答を受け入れるようにマークしてください:-) –

+0

申し訳ありません - 私は1つしか選択できません!私は報酬の方法であなたをupvoted ... – snowangel

関連する問題