I have the following input in form (simple_form)
<%= simple_form_for(@foo) do |f| %>
<% f.input_field :bar, :collection => { 'Opt1' => 1, 'Opt2' => 2}, :include_blank => '(All)', :multiple => true %>
<% end %>
The HTML output for this form would be:
<option value="">(All)</option>
<option value="1"Opt1</option>
<option value="2">Opt2</option>
I would like to set the (All)
option as :selected
, but (All)
has a value of ""
.
I tried,
<% f.input_field :bar, :collection => { 'Opt1' => 1, 'Opt2' => 2}, :include_blank => '(All)', :multiple => true, :selected => '' %>
I also tried nil
, ""
, " "
, etc.
Is there a way to set :selected
on the option that has been set in :include_blank
?
Have you tried setting :bar
to nil?
I have not tried setting :bar
to null. What impact do you think that would have? I’m not sureI follow how that might change the selection for an option in the input_field?
So when I add :include_blank
to a field it will have that as the default selected value for a new object. However, when editing a object it will set the select box to whatever has been chosen.
I presume you are trying to set the attribute :foo
to blank. If :foo
is set to something already, simple_form may be setting the select box to that value. When you run the code which option is selected?
Thanks for the feedback. The default selection based off of what :foo
is set to is not a factor because I am setting :mutliple => true
. With mutliple: => true
, there is no longer a default selection based off of the testing that I have done.
Oh, is :foo
an attribute your persisting to the database? With multiple: true
there should most certainly be selected values so users are made aware of the selection they have made if they go back to edit the model.
If you are able to select multiple options, should there even be a blank value?
One last thing you could try, I think :multiple
expects an array of values. You could try selected: ['']
i.e.
<% f.input_field :bar, :collection => { 'Opt1' => 1, 'Opt2' => 2}, :include_blank => '(All)', :multiple => true, :selected => [''] %>
Let me know if that works.