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?
This might work:
options = Carmake.all.unshift Carmake.new(id: 0, name: 'Any')
collection_select(:service, :carmake_id, options, :id, :name, include_blank: 'Any')
Although I did not test saving/updating in action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With