Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove a blank option of select field that was generated by SimpleForm

I have this piece of code:

= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"] 

Choices["Categories"] is just a hash of key=>value pairs.

SimpleForm generates a select field with all needed options, but it also makes the first option blank.
This blank option is present in all select fields that were generated by SimpleForm.

But I don't want to have a blank option. Is there a way to get rid of it?

Something like :allow_blank_option => false?

I tried to make a presence validation of this attribute hoping that SimpleForm will detect it, but it didn't help.

like image 949
Serge Vinogradoff Avatar asked Feb 28 '13 18:02

Serge Vinogradoff


2 Answers

You can pass a include_blank: false, include_hidden: false option:

= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"], include_blank: false, include_hidden: false 
like image 76
Dylan Markow Avatar answered Oct 12 '22 09:10

Dylan Markow


or you can customize call back action in your model to remove any empty string in the array parameter, assuming a parameter with the name "types":

before_validation :remove_empty_string  def remove_empty_string   types.reject! { |l| l.empty? } end 
like image 23
Antonio Jha Avatar answered Oct 12 '22 08:10

Antonio Jha