请帮我解决collection_select的问题。 当我使用时:
collection_select(:service, :carmake_id, Carmake.all, :id, :name, include_blank: 'Any')
HTML是:
<select id="service_carmake_id" name="service[carmake_id]">
<option value="">Any</option>
<option value="12">Audi</option>
<option value="16">Porsche</option>
<option value="17">VW</option>
</select>
但是对于“Any”选项我需要value =“0”。 有可能吗?
更新
select(:service, :carmake_id, [['Any', 0]] + Carmake.all.collect { |p| [p.name, p.id]})
帮了我,但是有铁路?或者我误解了什么?
答案 0 :(得分:14)
这可能有效:
options = Carmake.all.unshift Carmake.new(id: 0, name: 'Any')
collection_select(:service, :carmake_id, options, :id, :name, include_blank: 'Any')
虽然我没有测试保存/更新的实际效果。