Ruby on Rails: Where to define global constants?

前端 未结 12 1758
我在风中等你
我在风中等你 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:24

    Use a class method:

    def self.colours
      ['white', 'red', 'black']
    end
    

    Then Model.colours will return that array. Alternatively, create an initializer and wrap the constants in a module to avoid namespace conflicts.

提交回复
热议问题