我刚刚开始使用我的第一个Ruby on Rails web应用程序。我有很多不同的模型,视图,控制器等等。

我想找到一个好地方粘真正的全局常数的定义,适用于我的整个应用程序。特别是,它们适用于我的模型的逻辑,并在我的视图中所做的决定。我找不到任何DRY的地方来放置这些定义,它们既适用于我的所有模型,也适用于我的所有视图。

举个具体的例子,我想要一个常量colors = ['white', 'blue', 'black', 'red', 'green']。这在模型和视图中都被广泛使用。我可以在哪里只在一个地方定义它,以便它是可访问的?

我的尝试:

Constant class variables in the model.rb file that they're most associated with, such as @@COLOURS = [...]. But I couldn't find a sane way to define it so that I can write in my views Card.COLOURS rather than something kludgy like Card.first.COLOURS. A method on the model, something like def colours ['white',...] end - same problem. A method in application_helper.rb - this is what I'm doing so far, but the helpers are only accessible in views, not in models I think I might have tried something in application.rb or environment.rb, but those don't really seem right (and they don't seem to work either)

是否没有办法定义从模型和视图都可以访问的东西?我的意思是,我知道模型和视图应该是分开的,但在某些领域,它们肯定会有需要引用相同的领域特定知识的时候?


当前回答

如果一个常量在多个类中需要,我会把它放在config/initializers/constant中。Rb总是全大写(下面的状态列表被截断)。

STATES = ['AK', 'AL', ... 'WI', 'WV', 'WY']

除了在模型代码中,它们可以在整个应用程序中使用:

    <%= form.label :states, %>
    <%= form.select :states, STATES, {} %>

要在模型中使用该常量,请使用attr_accessor使该常量可用。

class Customer < ActiveRecord::Base
    attr_accessor :STATES

    validates :state, inclusion: {in: STATES, message: "-- choose a State from the drop down list."}
end

其他回答

使用类方法:

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

然后模型。colors将返回该数组。或者,创建初始化式并将常量包装在模块中,以避免名称空间冲突。

我认为您可以使用gem配置

https://github.com/rubyconfig/config

易于操作和编辑

对于应用程序范围的设置和全局常量,我建议使用Settingslogic。这些设置存储在YML文件中,可以从模型、视图和控制器中访问。此外,您可以为所有环境创建不同的设置:

  # 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

在视图的某个地方(我更喜欢这种东西的帮助方法)或在模型中,你可以得到,例如,数组的颜色Settings.colors.split(/\s/)。它非常灵活。你不需要发明一辆自行车。

从Rails 4.2开始,您可以使用配置。x属性:

# config/application.rb (or config/custom.rb if you prefer)
config.x.colours.options = %w[white blue black red green]
config.x.colours.default = 'white'

将以以下方式提供:

Rails.configuration.x.colours.options
# => ["white", "blue", "black", "red", "green"]
Rails.configuration.x.colours.default
# => "white"

加载自定义配置的另一种方法:

# config/colours.yml
default: &default
  options:
    - white
    - blue
    - black
    - red
    - green
  default: white
development:
  *default
production:
  *default
# config/application.rb
config.colours = config_for(:colours)
Rails.configuration.colours
# => {"options"=>["white", "blue", "black", "red", "green"], "default"=>"white"}
Rails.configuration.colours['default']
# => "white"

在Rails 5、6和7中,除了config.x,您还可以直接使用配置对象进行自定义配置。但是,它只能用于非嵌套配置:

# config/application.rb
config.colours = %w[white blue black red green]

它将以以下方式提供:

Rails.configuration.colours
# => ["white", "blue", "black", "red", "green"]

Rails 6.1引入了可以简化YML配置的共享组:

# config/colours.yml
shared:
  options:
    - white
    - blue
    - black
    - red
    - green
  default: white

全局变量应该在config/initializers目录中声明

COLOURS = %w(white blue black red green)