有人能解释一下这到底是怎么回事吗?
问题:
我有一个Scaffold并运行迁移:
rails g migration AddRarityToTags rarity:string对于稀有输入,我需要一个下拉列表,显示可供选择的选项列表。
e.g. Rarity = Free
Common
Rare
Epic如果我是对的,我需要这样的东西:
select_tag :rarity, options_for_select(@rarity)我找了很多,但没有多大帮助,我更困惑了。
有人能帮我吗?
发布于 2013-12-01 14:33:10
假设将原始选项作为字符串放入标记中:
select_tag :rarity, '<option>Free</option><option>Common</option>...'如果传入一个数组,这就是options_for_select返回的内容:
select_tag :rarity, options_for_select(['Free', 'Common', ...])请参阅:select
要强制执行“哑视图”规则,并让其他人使用该数组,您可以将其移动到模型中:
class Tag < ActiveRecord::Base
RARITY_LEVELS = %w(Free Common Rare Epic)
end..。
select_tag :rarity, options_for_select(Tag::RARITY_LEVELS)我和OP都想知道Rails或任何宝石是否让我们获得比这个更多的DRYer;例如,Rails允许我们直接将RARITY_LEVELS附加到rarity字段.
https://stackoverflow.com/questions/20313065
复制相似问题