Felipe Marcon
Felipe Marcon

Reputation: 349

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.

Upvotes: 1

Views: 1377

Answers (2)

Clemens Kofler
Clemens Kofler

Reputation: 1968

It is not possible to select a disabled element (or disable a selected element – if you're looking at it this way).

Upvotes: 0

Tarek N. Elsamni
Tarek N. Elsamni

Reputation: 1837

  • 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

Upvotes: 1

Related Questions