配置 session_store.rb 来处理登台和生产?

发布于 2024-12-01 05:42:05 字数 708 浏览 0 评论 0原文

我的 Rails 3.1rc6 应用程序上有一个使用子域的暂存和生产环境。我为这些环境购买并配置了不同的域名,因为默认的 some-something.herokuapp.com 不能很好地与子域配合。

当我将 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?

哪个不起作用。

I have a staging and a production environment on my rails 3.1rc6 app which uses subdomains. I've bought and configured different domain names for these environments, because the default something-something.herokuapp.com doesn't play nicely with subdomains.

When I set session_store.rb to this for one environment, everything works fine:

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

But I can't seem to add in a conditional to allow for the environment-specific domain names.

I've tried

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?

which doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2024-12-08 05:42:05

以下设置对我来说工作正常:

config/environments/staging.rb

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

config/environments/development.rb

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

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'
在风中等你 2024-12-08 05:42:05

您可以使用 :domain =>; :all 选项。您还可以提供 :tld_length(如果不同于 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)
      ".#{
amp;}"
    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

否则,您还应该能够覆盖 config/environments/ENVIRONMENT.rb 中的设置code> 每个环境的文件。

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)
      ".#{
amp;}"
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文