Can I include blank field in select_tag?

前端 未结 4 1931
栀梦
栀梦 2020-12-17 08:58

Is it possible to add an option like :include_blank => \'Please Select\' in a method select_tag like it is possible with the select

相关标签:
4条回答
  • 2020-12-17 09:16

    In Rails 3 there is a :prompt option for select_tag:

    select_tag "things", many_thing_as_options, :prompt => "Please select"
    
    0 讨论(0)
  • 2020-12-17 09:18

    Note that in Ruby on Rails 3, select_tag() will accept an :include_blank boolean argument (same with date_select and the like).

    0 讨论(0)
  • 2020-12-17 09:23

    By passing include_blank: true or prompt: 'Please select' as a third parameter to select_tag

    <%= select_tag "individual[address][state]", options_for_select(states), { include_blank: true, class: 'form-control' } %>
    

    For prompt, you can use like this with select_tag

    <%= select_tag "individual[address][state]", options_for_select(indian_states), { prompt: 'Please select', class: 'form-control' } %>
    
    0 讨论(0)
  • 2020-12-17 09:24

    The select_tag method does not modify the options list you pass it. If you want a blank option you have to include it in your list of options.

    If you're using options_for_select your list should start with the blank item, ie: ["Please select", ''].

    If you're just passing html to select_tag make sure your first option is:

    <option value="">Please Select</option>
    
    0 讨论(0)
提交回复
热议问题