Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 在rails应用程序中,我将在哪里存储静态/常量值?_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 在rails应用程序中,我将在哪里存储静态/常量值?

Ruby on rails 3 在rails应用程序中,我将在哪里存储静态/常量值?,ruby-on-rails-3,Ruby On Rails 3,我有rails应用程序,我想知道存储常量的最佳位置 例如: HELLO_EVERYONE = "hiz" 然后在一些控制器和视图中: arr_used = [HELLO_EVERYONE] 这取决于您需要访问它们的位置 如果您需要在整个应用程序中使用它们,可以将它们放在environment.rb # environment.rb # # other global config info HELLO_EVERYONE = "hiz" 如果只需要在特定类中访问它们,可以在该模型中定义它们 c

我有rails应用程序,我想知道存储常量的最佳位置

例如:

HELLO_EVERYONE = "hiz"
然后在一些控制器和视图中:

arr_used = [HELLO_EVERYONE]

这取决于您需要访问它们的位置

如果您需要在整个应用程序中使用它们,可以将它们放在
environment.rb

# environment.rb
#
# other global config info
HELLO_EVERYONE = "hiz"
如果只需要在特定类中访问它们,可以在该模型中定义它们

class Test < ActiveRecord::Base
  HELLO_EVERYONE = "hiz"
end
class Address< ActiveRecord::Base
  STATES = ["AL", "AK", "AZ", "AR", ...]

  def self.states
    STATES
  end
end