枚举,如何在simple_form中显示多个单词?
Enum, how to display multiple word in simple_form?
我正在为 :discount_type
使用枚举。枚举是 percent
或 price
。
为了清楚起见,我想在枚举 price
的 simple_form 中显示 price per day
。我该怎么做?
代码
型号
enum discount_type: { percent: 1, price: 0 }
表格
<%= f.input :discount_type, collection: ['percent', 'price'] %>
上次尝试
model
enum discount_type: { percent: 1, price_per_day: 0 }
form
<%= f.input :discount_type, collection: ['percent', 'price per day'] %>
error message:
==> 'price per day' is not a valid discount_type
来自文档:
The mappings are exposed through a class method with the pluralized
attribute name, which returns the mapping in a
HashWithIndifferentAccess...
这有点难看,因为 per day
添加到 price
:
Model.discount_types.transform_keys { |key| key == 'price' ? 'price per day' : key }.keys
# ["percent", "price per day"]
因此,在您的表单中:
<%= f.input :discount_type, collection: Model.discount_types.transform_keys { |key| key == 'price' ? 'price per day' : key }.keys %>
我正在为 :discount_type
使用枚举。枚举是 percent
或 price
。
为了清楚起见,我想在枚举 price
的 simple_form 中显示 price per day
。我该怎么做?
代码
型号
enum discount_type: { percent: 1, price: 0 }
表格
<%= f.input :discount_type, collection: ['percent', 'price'] %>
上次尝试
model
enum discount_type: { percent: 1, price_per_day: 0 }
form
<%= f.input :discount_type, collection: ['percent', 'price per day'] %>
error message:
==> 'price per day' is not a valid discount_type
来自文档:
The mappings are exposed through a class method with the pluralized attribute name, which returns the mapping in a HashWithIndifferentAccess...
这有点难看,因为 per day
添加到 price
:
Model.discount_types.transform_keys { |key| key == 'price' ? 'price per day' : key }.keys
# ["percent", "price per day"]
因此,在您的表单中:
<%= f.input :discount_type, collection: Model.discount_types.transform_keys { |key| key == 'price' ? 'price per day' : key }.keys %>