Rails相当于Django的“选择”

时间:2021-11-18 12:33:01

I know there is no real equivalent in Rails but my question is mostly about best practice...

我知道Rails中没有真正的等价物,但我的问题主要是关于最佳实践......

In Django, if you want to limit a model field to a limited set of choices, you would do something like this (in your model):

在Django中,如果要将模型字段限制为有限的选择集,则可以执行以下操作(在模型中):

COLOR_CHOICES = (('B', 'Blue'), ('R', 'Red'))
item_color = models.CharField(choices=COLOR_CHOICES)

From my (basic) understanding of Rails, I can achieve something similar, for example, by using a select tag in the forms dealing with adding/editing that model...

根据我对Rails的基本理解,我可以实现类似的功能,例如,在表单中使用select标签来处理添加/编辑该模型...

My question however is, where would it be appropriate to declare the "choices" hash (again I'm guessing here that a hash is what I need?). Basically I just want it to be easily re-usable in any forms where I might need to present those choices, and when it comes to validating at the model level.

然而我的问题是,在哪里声明“选择”哈希是合适的(我再次猜测哈希是我需要的?)。基本上我只是希望它能够以任何形式轻松地重复使用,我可能需要提供这些选择,以及在模型级别进行验证时。

Any help/tips would be appreciated!

任何帮助/提示将不胜感激!

2 个解决方案

#1


10  

On the validation side of things, probably validates_inclusion_of is what you need:

在验证方面,可能validates_inclusion_of就是您所需要的:

class Coffee < ActiveRecord::Base
  validates_inclusion_of :size, :in => %w(small medium large),
    :message => "%{value} is not a valid size"
end

As for generating the helper, you can try something like:

至于生成帮助程序,您可以尝试以下方法:

class Coffee < ActiveRecord::Base
  @@coffe_size = %w(small medium large)

  validates_inclusion_of :size, :in => @@coffe_size,
    :message => "%{value} is not a valid size"

   def self.coffee_size_options
       @@coffe_size.map{ |z| [z,z]} 
   end
end

And then in some helper:

然后在一些帮手:

<%= select(:coffee, :size, Coffee.coffee_size_options) %>

#2


0  

2 years later, there's a better option: values_for

2年后,有一个更好的选择:values_for

class Car < ActiveRecord::Base
  attr_accessible :brand
  values_for :brand, :has=>[:ford, :chevy, :dodge], :add=>[:constants]

  def doStuff
    # Now you can...
    Car.brands # [:ford, :chevy, :dodge]
    Car::BRAND_FORD # "ford"
    myCar = Car.new(:brand=>Car::BRAND_FORD)
    myCar.valid? # true
    myCar.brand= "duck."
    myCar.valid? # false
  end
end

#1


10  

On the validation side of things, probably validates_inclusion_of is what you need:

在验证方面,可能validates_inclusion_of就是您所需要的:

class Coffee < ActiveRecord::Base
  validates_inclusion_of :size, :in => %w(small medium large),
    :message => "%{value} is not a valid size"
end

As for generating the helper, you can try something like:

至于生成帮助程序,您可以尝试以下方法:

class Coffee < ActiveRecord::Base
  @@coffe_size = %w(small medium large)

  validates_inclusion_of :size, :in => @@coffe_size,
    :message => "%{value} is not a valid size"

   def self.coffee_size_options
       @@coffe_size.map{ |z| [z,z]} 
   end
end

And then in some helper:

然后在一些帮手:

<%= select(:coffee, :size, Coffee.coffee_size_options) %>

#2


0  

2 years later, there's a better option: values_for

2年后,有一个更好的选择:values_for

class Car < ActiveRecord::Base
  attr_accessible :brand
  values_for :brand, :has=>[:ford, :chevy, :dodge], :add=>[:constants]

  def doStuff
    # Now you can...
    Car.brands # [:ford, :chevy, :dodge]
    Car::BRAND_FORD # "ford"
    myCar = Car.new(:brand=>Car::BRAND_FORD)
    myCar.valid? # true
    myCar.brand= "duck."
    myCar.valid? # false
  end
end