在 Rails 3 中放置全局变量的位置 [英] Where to put Global variables in Rails 3

查看:36
本文介绍了在 Rails 3 中放置全局变量的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用 Rails 2.3.8 应用程序将全局变量放在 environment.rb 中,例如:

I used to put Global variables in environment.rb with my Rails 2.3.8 application such as:

MAX_ALLOWD_ITEMS = 6

它似乎在 Rails 3 中不起作用.我尝试将它放在 application.rb 中,但没有帮助.

It doesn't seem to work in Rails 3. I tried putting it in application.rb and that didn't help.

你有什么建议?

推荐答案

如果您已经按照 Ryan 的建议尝试重新启动服务器,请尝试将其放入您的 application.rb 中,如下所示:

If you have already tried restarting your server as Ryan suggested, try putting it in your application.rb like this:

module MyAppName
  class Application < Rails::Application
    YOUR_GLOBAL_VAR  = "test"
  end
end

然后你可以在你的控制器、视图或任何地方使用命名空间调用它..

Then you can call it with the namespace in your controllers, views or wherever..

MyAppName::Application::YOUR_GLOBAL_VAR

另一种选择是使用类似 settingslogic 的东西.使用 settingslogic,您只需创建一个 yml 配置文件和一个指向配置文件的模型 (Settings.rb).然后,您可以在 Rails 应用中的任何位置访问这些设置:

Another alternative would be using something like settingslogic. With settingslogic, you just create a yml config file and a model (Settings.rb) that points to the config file. Then you can access these settings anywhere in your rails app with:

Settings.my_setting

这篇关于在 Rails 3 中放置全局变量的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆