带导轨 4
像这样:
<%= collection_select(:template, :template_id, @templates, :id, Proc.new {|v| v.name.humanize}) %>
在Proc
块中,v
将是您的模型,通过each
循环进行迭代。
collection_select
适用于从 Proc 的内容继承Enumerable
且没有限制的任何内容。
使用导轨 3
您必须自己做,通过在将数据传递给之前准备好您的数据collection_select
# This will generate an Array of Arrays
values = @templates.map{|t| [t.id, t.name.humanize]}
# [ [record1.id, record1.name.humanize], [record2.id, record2.name.humanize], ... ]
# This will use the Array previously generated, and loop in it:
# - send :first to get the value (it is the first item of each Array inside the Array)
# - send :last to get the text (it is the last item of each Array inside the Array)
# ( we could have use :second also )
<%= collection_select(:template, :template_id, values, :first, :last) %>