8

Help me please to solve problem with collection_select. When I use:

collection_select(:service, :carmake_id, Carmake.all, :id, :name, include_blank: 'Any')

HTML is:

<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>

But I need value="0" for "Any" option. Is it possible?

Update:

select(:service, :carmake_id, [['Any', 0]] + Carmake.all.collect { |p| [p.name, p.id]})

helped me, but there is railsway? Or I misunderstand something?

4

1 回答 1

14

这可能有效:

options = Carmake.all.unshift Carmake.new(id: 0, name: 'Any')
collection_select(:service, :carmake_id, options, :id, :name, include_blank: 'Any')

虽然我没有测试保存/更新操作。

于 2013-09-06T14:13:50.570 回答