Kreeki
Kreeki

Reputation: 3732

Prompt in select_tag

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

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

And I want to add a prompt as a first default value (something like "--- select country ---"). Where and how should I put this option?

Upvotes: 20

Views: 28825

Answers (4)

longpc
longpc

Reputation: 71

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

Upvotes: 7

Jed Schneider
Jed Schneider

Reputation: 14671

Use the FormHelper :prompt

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

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

Upvotes: 38

user1875926
user1875926

Reputation: 131

You can also give customized prompt value like this

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

Upvotes: 4

gunn
gunn

Reputation: 9165

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.

Upvotes: 2

Related Questions