Where is the best place to store application parameters : database, file, code…?

后端 未结 5 1667
礼貌的吻别
礼貌的吻别 2021-01-03 10:24

I am developing a Ruby on Rails website and I have an \"architectural\" question : my application needs some parameters and I\'m wondering where to store them.

In co

5条回答
  •  误落风尘
    2021-01-03 10:54

    (Have since viewed that rails cast mentioned above [episode 85] - it looks like a bit more 'the rails way' than below)

    Another approach is to build on the existing configuration mechanism in Rails. Lets presume there are two types of configs:

    1. App wide configs common to dev/test/prod environments
    2. Configs specific to envrionments dev/test/prod

    For the first scenario, items in "RAILS_ROOT + '/config/environment.rb'" work. Just see that the names are captialised so they are Ruby constants. A variation to this is have a reference to another file in that environment.rb file ...

    require RAILS_ROOT + '/config/appConfigCommon.rb'
    

    and place relevant config items in that file. This has the advantage of being able to be referenced independant of Rails.

    For scenario 2, a similar approach can be taken. Place items for development in "RAILS_ROOT + '/config/environments/development.rb'" or something like

    require RAILS_ROOT + '/config/environments/appConfigDev.rb'
    

    and place environment specific items in that required file, making sure they start with caps. And follow the same pattern for test/prod (and others if need be).

    The config items are directly accessible in views and controllers (not sure about models) by simply using the constant name.

提交回复
热议问题