Prompt in select_tag

后端 未结 4 653
醉话见心
醉话见心 2021-02-07 07:31

In my application in user registration I have a country picker..

<%= select(:user, :country, options_for_select(@COUNTRIES)) %>

And I wan

相关标签:
4条回答
  • 2021-02-07 08:17
    collection_select(:product,
      :category_id,
      Category.all,    
      :id,    
      :title,    
      {:prompt => true}
    )
    
    collection_select(:product,    
      :category_id,    
      Category.all,    
      :id,    
      :title,    
      {:include_blank => 'Please Select'}
    )
    

    both of these result in the same html, but the first one will not include the 'Please Select' option when you return to edit the previously created Product

    0 讨论(0)
  • 2021-02-07 08:18

    Use the FormHelper :prompt

    select(:user, :country, options_for_select(@COUNTRIES), {:prompt => "--select county--"})
    

    http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper

    0 讨论(0)
  • 2021-02-07 08:23

    Very simple:

    select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
    

    For the prompt "Please select", or this for your custom text:

    select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"Select country")
    

    Also note that @COUNTRIES is wrong, an instance variable should be lowercase - @countries, a contant would just be COUNTRIES.

    0 讨论(0)
  • 2021-02-07 08:26

    You can also give customized prompt value like this

    select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"select User name")
    
    0 讨论(0)
提交回复
热议问题