I used to put Global variables in environment.rb with my Rails 2.3.8 application such as:
我曾经使用我的Rails 2.3.8应用程序将Global变量放在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?
你有什么建议?
5 个解决方案
#1
47
If you have already tried restarting your server as Ryan suggested, try putting it in your application.rb
like 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
#2
7
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放在任何地方作为函数。
#3
2
If you are truly defining it in config/environment.rb like you say, the only way I can duplicate your problem is by running up a server using rails server
, then putting 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服务器运行服务器,然后将变量放入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.
如果我停止服务器并再次启动它并再次尝试访问该视图或控制器,那么它可以工作。我估计你还没有重新启动你的服务器。
#4
1
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)文件。记得每次更改任何内容时都要重新启动服务器。
#5
0
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。或者至少那是我在实验时发现的事情。我还认为使用初始化器/文件夹的建议在易用性方面可能是更好的解决方案。请参阅Rails中的永久变量
#1
47
If you have already tried restarting your server as Ryan suggested, try putting it in your application.rb
like 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
#2
7
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放在任何地方作为函数。
#3
2
If you are truly defining it in config/environment.rb like you say, the only way I can duplicate your problem is by running up a server using rails server
, then putting 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服务器运行服务器,然后将变量放入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.
如果我停止服务器并再次启动它并再次尝试访问该视图或控制器,那么它可以工作。我估计你还没有重新启动你的服务器。
#4
1
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)文件。记得每次更改任何内容时都要重新启动服务器。
#5
0
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。或者至少那是我在实验时发现的事情。我还认为使用初始化器/文件夹的建议在易用性方面可能是更好的解决方案。请参阅Rails中的永久变量