I don't think this is the case, but when creating a select box from an enum, make a note of how to change the wording.
As a premise, ʻenumerizeand
reform` are used for the gem.
product_form.rb
extend Enumerize
enumerize :fluit, in: {
apple: 1,
orange: 2,
peach: 3,
strawberry: 4,
cherry: 5,
}, predicates: { prefix: true }
property :fluit
ja.yml
product:
fluit:
apple:Apple
orange:Orange
peach:Peaches
strawberry:Strawberry
cherry:Cherry
If you write it normally, this is fine.
new.slim
= f.select :fluit, f.object.class.fluit.options, {prompt: 'Select fruit'}
However, suppose that he was told, "I want to put the words" (on special sale) "for" apples "and" cherries ".
That said, I don't want to change ja.yml.
Writing one by one without using f.object.class.fluit.options
is also poorly maintainable.
Therefore, I created a method that turns the Array generated by f.object.class.fluit.options
with a map and adds the wording and returns only when it is a specific value.
product_form.rb
def fruit_select_display
self.class.fluit.options.map do |key, value|
if value == 'apple' || value == 'cherry'
[key + '(During special sale)', value]
else
[key, value]
end
end
end
new.slim
= f.select :fluit, f.object.fruit_select_display, {prompt: 'Select fruit'}
With this, I was able to use it once.
Recommended Posts