简体   繁体   中英

Ruby on Rails - First option of select in Form_for selected and disabled

How can I make first option of select options_from_collection_for_select as selected and disabled?

I have that code:

<%= f.select :state, options_from_collection_for_select(Estate.order(:name), :uf, :name), {}, :id => 'populate_cities' %>

And I need that output be:

<select id="populate_cities" name="guide_dog_form[state]"><option value="AC">Acre</option>
<option value="Select one option" disabled selected>Select one option</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>

I searched and found some answers, but nothing helped me.

Someone can help me? Thanks.

  • Use :prompt => "Placeholder" if you want the placeholder to show up only when the attribute is nil at the time the form is rendered. It will be selected by default, but nothing will be saved if user submits. If the attribute is already populated [possibly because a) there's a default value or b) it's an edit form], the placeholder item will be omitted from the list entirely.
  • Use :include_blank => "Placeholder" if you want to include the placeholder in the rendered list at all times.

as per: https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

无法选择禁用的元素(或禁用所选元素 - 如果您以这种方式查看)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM