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

前端 未结 3 2050
闹比i
闹比i 2021-02-03 17:48

I have this piece of code:

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

Choices[\"Cate

相关标签:
3条回答
  • 2021-02-03 17:51

    To remove a blank field from select it is necessary to show the selected so add selected: 1 Then set prompt to anything like prompt: "Please Select" The final output will be <%= select("social_links",:option_id, Option.all.collect {|p| [ p.name, p.id ] },{ selected: 1 , prompt: "Please Select"}, { class: 'form-control' , required:true})%>

    0 讨论(0)
  • 2021-02-03 18:05

    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
    
    0 讨论(0)
  • 2021-02-03 18:14

    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
    
    0 讨论(0)
提交回复
热议问题