在现有代码库中,discount
模型的一个属性是 discount_type
。由于该系统中仅使用两种类型的折扣(百分比和现金),因此它们在整个系统中被硬编码为 percentage
或 cash
,因此没有 discount_types
表或任何要映射到的内容。
在表单中,有以下代码:
=form_for @discount do |f|
...
=f.select :discount_type, options_for_select(["percentage", "cash"])
...
这对于新折扣非常有用,但是当拉出表单来编辑折扣时,无论 discount
对象的 discount_type 是什么,始终会选择
是。我们如何让表单默认为正在编辑的对象的 percentage
discount_type
?
最佳答案
不简单
= f.select :discount_type, ["percentage", "cash"]
够了吗?
关于ruby-on-rails - Rails 表单选择助手在编辑中填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585199/