【问题标题】:global constant not updating with correct values全局常量没有用正确的值更新
【发布时间】:2015-05-15 23:08:17
【问题描述】:

在 Rails 4 中,我有一个 config/config.yml,我想将它与 config/secrets.yml 分开。我更新了配置以包含 twilio 信息:

development:
  sendgrid:
    username: username
    password: password
    domain: domain
  twilio:
    account_sid: account_sid
    auth_token: auth_token
    twilio_number: twilio_number

在 config/application.rb 中,我加载了全局常量(我无法在初始化程序中加载它,因为我需要在环境/*rb 中使用它,并且环境/*rb 在初始化程序之前加载)。这是全局常量:

module RailsDevise
  class Application < Rails::Application
    ...
    config.before_configuration do
      ::APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
    end
  end
end

当我加载控制台时,更改不会反映:

> APP_CONFIG
 => {"sendgrid"=>{"username"=>"username", "password"=>"password", "domain"=>"domain"}, "twilio"=>{"account_sid"=>"account_sid", "auth_token"=>"auth_token"}} 

但是,他们肯定在那里:

> YAML.load(File.open("#{Rails.root}/config/config.yml"))
{"development"=>{"sendgrid"=>{"username"=>"username", "password"=>"password", "domain"=>"domain"}, "twilio"=>{"account_sid"=>"account_sid", "auth_token"=>"auth_token", "twilio_number"=>"twilio_number"}}

可以进行什么样的缓存?我重新加载了几次控制台,它应该重新加载整个应用程序。但它没有检测到 yml 文件中的更改。

【问题讨论】:

  • 为什么我在mac上关闭终端并重新打开它现在可以工作了。我不明白终端(可能还有一个正在运行的 ruby​​ 进程)是如何链接到这个 ruby​​ 变量的。
  • 您是否有可能在后台运行诸如 spring 之类的东西?
  • @JedSchneider 我曾在后台运行同步 gem,但仍不确定这对这个问题有何影响。
  • 那么,如果你运行spring status,你什么都得不到?
  • @smathy 现在我得到“春天没有运行”。但是我在退出并重新打开终端后尝试了您所说的。重新打开终端后,变量正确更新。所以很明显是一些正在运行的进程导致了这个问题。我不知道春天是什么。但我确实有一个同步过程正在运行:spawn "rackup sync.ru -E production"

标签: ruby-on-rails ruby


【解决方案1】:

sync 不会影响你自己的rails console,但spring 会。 spring 基本上在后台运行并将您的应用程序驻留在内存中,以便更快地(重新)启动rails console,或运行测试等。它会检测文件更改并自行重新启动,但它无法监视所有内容,并且绝对不会监视您自己的自定义 YAML 配置文件中的更改。让spring 运行将准确描述您的症状,它是默认 Gemfile 的一部分。

当这些事情再次发生时:spring stop 再试一次。

【讨论】:

  • 我的 Gemfile 中确实有 Spring。如果 spring 正在运行,有没有办法在控制台中检查?我什至没有意识到春天在那里。
  • spring status 正如我在评论中提到的那样。
  • 很高兴知道我的预感是对的! spring stop spring start 也可以。
  • FWIW,你不需要手动spring start - 下次你使用任何使用弹簧的东西时,它会自动为你启动弹簧。
【解决方案2】:

我使用的是 Rails 5,我遇到了同样的问题,清除缓存有效。

rake tmp:cache:clear

【讨论】:

    猜你喜欢
    • 2014-08-06
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    相关资源
    最近更新 更多