views:

103

answers:

2

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?

A: 

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.

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.

Ryan Bigg
A: 

If you have already tried restarting your server as Ryan suggested, try putting it in your applicaton.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 whatever..

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
cowboycoded