1

I have to use a constant inside my /app/config/environments/production.rb. I'd like to put it somewhere as configuration constant. So I tried to put it inside an initializer file, but /app/config/environments/production.rb seems to be loaded before. Then I tried to put my constant inside Rails::Initializer.run do |config|... inside the /app/config/environment.rb, but /app/config/environments/production.rb still seems to be loaded before. Where can I put propermy this constant as a config constant so that /app/config/environments/production.rb recognizes it ?

4

1 回答 1

3

让我们弄清楚您通过什么路径到达您的应用程序加载初始化文件。你必须记住这一点:

当您运行rails s(从您的app/script/rails文件)运行rails服务器时,

  • 需要您的应用程序boot.rb文件(主要处理 gems 加载),然后将您的控制台args(此处为serveralias s)要求传递给该rails railties/lib/rails/command.rb文件(位于rails源代码中)。

  • 在那里,您的应用程序config/application.rb是必需的。然后,Server类被实例化,你被扔到railties/lib/rails/command/server.rb文件中。事实上,这个Server类继承自Rack::Server,所以一个railsapp 就是rackapp !因此,您会config.ru在您的应用程序中找到一个文件rails,这会加载该config/environment.rb文件。

config/environment.rb文件做了两件事

  • 需要您的应用程序config/applcation.rb,加载正确的rails framework。(有关信息,您会在您的应用程序中找到继承自application.rbApplication类,该类继承自Rails::Application,该类Rails::Engine继承自一个Rails::Railtie类)

  • 调用initialize!bang 方法,加载config/environment/*.rb文件,然后处理所有初始化程序进程(更准确地说,初始化程序方法定义在railties/lib/rails/engine.rb)文件中。因此,现在,考虑到这一点,您应该能够正确定义常量。

为了更好地理解初始化过程,我向您推荐这个伟大的railscast,它主要启发了这个答案。

于 2013-09-01T16:16:37.390 回答