Rails 相当于 Django 的“选择”

发布于 2024-10-07 14:13:07 字数 410 浏览 0 评论 0原文

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

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

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

根据我对 Rails 的(基本)理解,我可以实现类似的目标,例如,通过在处理添加/编辑该模型的表单中使用选择标签...

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

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

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

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):

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...

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后来的我们 2024-10-14 14:13:07

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

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

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

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

然后在某些帮助程序中:

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

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

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) %>
清晨说晚安 2024-10-14 14:13:07

您可以简单地使用枚举

class Coffee < ActiveRecord::Base
  enum color: [ :blue, :red, :green ]
end

更多信息: https://api .rubyonrails.org/v5.2.4.1/classes/ActiveRecord/Enum.html

You can simply use enum

class Coffee < ActiveRecord::Base
  enum color: [ :blue, :red, :green ]
end

More information here : https://api.rubyonrails.org/v5.2.4.1/classes/ActiveRecord/Enum.html

澉约 2024-10-14 14:13:07

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

2 years later, there's a better option: 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文