Can someone explain me how this exactly works ?
Problem:
I have a Scaffold and run the migration:
rails g migration AddRarityToTags rarity:string
For the rarity input i need a dropdown displaying a list of options to select from.
e.g. Rarity = Free
Common
Rare
Epic
If i'm right i need something like this:
select_tag :rarity, options_for_select(@rarity)
I searched a lot but it didn't helped much, i got more confused.
Can someone help me out ?
Imagine putting the raw options into the tag as a string:
select_tag :rarity, '<option>Free</option><option>Common</option>...'
That's what options_for_select
returns, if you pass in an array:
select_tag :rarity, options_for_select(['Free', 'Common', ...])
To enforce the "dumb views" rule, and to let others use that array, you can move it to the model:
class Tag < ActiveRecord::Base
RARITY_LEVELS = %w(Free Common Rare Epic)
end
...
select_tag :rarity, options_for_select(Tag::RARITY_LEVELS)
Both me and the OP would like to know if Rails, or any gems, let us get any DRYer than this; if, for example, Rails lets us attach the RARITY_LEVELS
directly to the rarity
field...