Ruby-on-rails 在 Rails 3 中放置全局变量的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3598785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put Global variables in Rails 3
提问by Tam
I used to put Global variables in environment.rb with my Rails 2.3.8 application such as:
我曾经使用 Rails 2.3.8 应用程序将全局变量放在 environment.rb 中,例如:
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.
它似乎在 Rails 3 中不起作用。我尝试将它放在 application.rb 中,但没有帮助。
What do you suggest?
你有什么建议?
回答by johnmcaliley
If you have already tried restarting your server as Ryan suggested, try putting it in your application.rblike this:
如果您已经按照 Ryan 的建议尝试重新启动服务器,请尝试将其放入application.rb如下所示:
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:
另一种选择是使用类似settingslogic 的东西。使用 settingslogic,您只需创建一个 yml 配置文件和一个指向该配置文件的模型 (Settings.rb)。然后你可以在你的 rails 应用程序中的任何地方访问这些设置:
Settings.my_setting
回答by bhakku
I usually go with application_helper.rb This is what it looks like:
我通常使用 application_helper.rb 这是它的样子:
module ApplicationHelper
def my_global_variable
my_global_variable = "Helloworld!"
end
end
Then I can put in my_global_variable anywhere as a function.
然后我可以将 my_global_variable 作为函数放入任何地方。
回答by Ryan Bigg
If you are truly defining it in config/environment.rblike you say, the only way I can duplicate your problem is by running up a server using rails server, thenputting in the variable to config/environment.rb, referencing it in a view or controller somewhere and then trying to load that specific part of my application.
如果您真的像您说的那样在config/environment.rb 中定义它,那么我可以复制您的问题的唯一方法是使用 运行服务器rails server,然后将变量放入config/environment.rb,在视图中引用它或控制器某处,然后尝试加载我的应用程序的特定部分。
If I stop the server and start it again and again try to access that view or controller then it works. I reckon you just haven't restarted your server.
如果我停止服务器并一次又一次地启动它,尝试访问该视图或控制器,然后它就可以工作了。我想你只是没有重新启动你的服务器。
回答by bmac
I normally create inside config/initializers/ a yaml (yml) file with all the global site settings. remember to restart the server each time you change anything.
我通常在 config/initializers/ 内部创建一个包含所有全局站点设置的 yaml (yml) 文件。请记住每次更改任何内容时重新启动服务器。
回答by sameers
I don't know if the solution of adding variables to environment.rb would in fact work in Rails3 - to be specific, if you haven't defined the variable inside a module definition like so:
我不知道将变量添加到 environment.rb 的解决方案是否实际上适用于 Rails3 - 具体来说,如果您没有像这样在模块定义中定义变量:
module MyConfig
Max_ints = 5
end
you won't be able to just use Max_ints, if you just include it as a bare definition. Or at least that's what I found happened when I experimented with this. I also think the suggestion to use the initializers/ folder is possibly a better solution in terms of ease of use. See Permanent variable in Rails
如果您只是将其作为裸定义包含在内,您将无法仅使用 Max_ints。或者至少这是我在试验时发现的情况。我还认为使用 initializers/ 文件夹的建议在易用性方面可能是更好的解决方案。请参阅Rails 中的永久变量

