Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails collection_select set value for include_blank

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?

like image 650
iAnton Avatar asked Sep 06 '13 13:09

iAnton


1 Answers

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.

like image 60
zwippie Avatar answered Nov 15 '22 07:11

zwippie