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

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

问题描述



  MAX_ALLOWD_ITEMS = 6我曾经在Global.rb中放入全局变量

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



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

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

然后你可以在你的控制器,视图或任何地方调用它。

  MyAppName :: Application :: YOUR_GLOBAL_VAR 

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

  Settings.my_setting 


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

MAX_ALLOWD_ITEMS = 6

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

What do you suggest?

解决方案

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

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天全站免登陆