Configure session_store.rb to handle staging and production?

拟墨画扇 提交于 2019-12-03 06:58:04
Simone Carletti

You can use the :domain => :all option. You can also provide a :tld_length, if different than 1.

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

Here's the relevant Rails code

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

Otherwise, you should also be able to override the settings in the config/environments/ENVIRONMENT.rb file on a per-environment basis.

The following settings has been working fine for me:

config/environments/staging.rb

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

config/environments/production.rb

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.myproductiondomain.com'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!