Ruby on Rails: Where to define global constants?

前端 未结 12 1783
我在风中等你
我在风中等你 2020-11-28 00:44

I\'m just getting started with my first Ruby on Rails webapp. I\'ve got a bunch of different models, views, controllers, and so on.

I\'m wanting to find a good plac

12条回答
  •  甜味超标
    2020-11-28 01:07

    For application-wide settings and for global constants I recommend to use Settingslogic. This settings are stored in YML file and can be accessed from models, views and controllers. Furthermore, you can create different settings for all your environments:

      # app/config/application.yml
      defaults: &defaults
        cool:
          sweet: nested settings
        neat_setting: 24
        awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
    
        colors: "white blue black red green"
    
      development:
        <<: *defaults
        neat_setting: 800
    
      test:
        <<: *defaults
    
      production:
        <<: *defaults
    

    Somewhere in the view (I prefer helper methods for such kind of stuff) or in a model you can get, for ex., array of colors Settings.colors.split(/\s/). It's very flexible. And you don't need to invent a bike.

提交回复
热议问题