How to Set a Blank Value for an F.Select Form Field

How do I set a blank value for an f.select form field

There are two possibilities, depending on what you're after:

include_blank

<%= f.select (:sex, %w{ Male Female }, :include_blank => true) %>

This will always include a blank option in the select, which will allow people to set the value back to the blank value if they're seeing this on an edit form.

prompt

<%= f.select (:sex, %w{ Male Female }, :prompt => "Gender...") %>

This will include the specified prompt value, so long as the field hasn't already been set. If it has (on an edit form for example), there's no need to remind the user that they need to select a value so the prompt doesn't appear

How can remove Null value from f.select in rails?

You have two problems there

Your collect is returning some nil elements(As Joseph said), in this case the name attribute is what it can be nil, so you can check for that on the collect

Solution(compact) [UPDATE]

f.select(:doc, file.all.collect {|a| [a.name, a.id] if a.name, include_hidden: false }.compact, {}, id: "id-select2", class: "form-control", :multiple => true)

Specify the include_blank option https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select

f.select(:doc, file.all.collect {|a| [a.name, a.id] if a.name }.compact, { include_blank: false, include_hidden: false }, id: "id-select2", class: "form-control", :multiple => true)

According to the docs for select helper I have found this gotcha

The HTML specification says when multiple parameter passed to select
and all options got deselected web browsers do not send any value to
server. Unfortunately this introduces a gotcha: if an User model has
many roles and have role_ids accessor, and in the form that edits
roles of the user the user deselects all roles from role_ids multiple
select box, no role_ids parameter is sent. So, any mass-assignment
idiom like To prevent this the helper generates an auxiliary hidden
field before every multiple select. The hidden field has the same name
as multiple select and blank value.

Note: The client either sends only the hidden field (representing the
deselected multiple select box), or both fields. This means that the
resulting array always contains a blank string.

In case if you don't want the helper to generate this hidden field you
can specify include_hidden: false option.

So if you add the include_hidden: false option then you won't get the empty string on your multiple option when the data is sent to the controller.

Rails 4 Make Default for f select null

As per the select,you need to write :include_blank => true as an argument of html_options

This should work

<%= f.select :user_ids, options_for_select(User.all.collect{|user| [user.name, user.id]}, @reservation.user_ids), {multiple: true, :include_blank => true,:class=>'user-id-widget', size: 5} %>

Select a blank option in the static drop-down rails form

Try this:

<%= f.select :point1, options_for_select([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]), {:include_blank => true} %>


Related Topics



Leave a reply



Submit