Where to put Global variables in Rails 3

后端 未结 5 1774
旧时难觅i
旧时难觅i 2020-11-30 03:25

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

5条回答
  •  清歌不尽
    2020-11-30 03:47

    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
    

提交回复
热议问题