Search code examples
ruby-on-rails-4

Where store or implement global variables ($) in Rails?


I want to use/implement global variables in Rails. I tried the following:

In config/environments/production.rb:

$n_exams_question = 20

In config/environments/development.rb:

$n_exams_question = 3

I want to use different values in production and development. My localserver is not detecting the global variables. Where is the best place to implement it?


Solution

  • Rails does it that way: https://guides.rubyonrails.org/configuring.html#custom-configuration

    In config/environments/production.rb:

    config.myGlobalVariable = 'blabla1'
    

    In config/environments/development.rb:

    config.myGlobalVariable = 'blabla2'
    

    then use this Anywhere:

    Rails.configuration.myGlobalVariable