Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a blank value for an f.select form field

I am using the following to allow my users to select their sex in their profile.

<%= f.select (:sex, %w{ Male Female }) %> 

How would I create a blank value that the list would default to if nothing has been passed to the user.sex column? I am simply passing male or female as a string.

The purpose is I want a blank value so a validation can make sure they are aware they have to select it.

like image 577
bgadoci Avatar asked Dec 14 '10 21:12

bgadoci


2 Answers

There are two possibilities, depending on what you're after:

include_blank

<%= f.select (:sex, %w{ Male Female }, :include_blank => true) %> 

This will always include a blank option in the select, which will allow people to set the value back to the blank value if they're seeing this on an edit form.

prompt

<%= f.select (:sex, %w{ Male Female }, :prompt => "Gender...") %> 

This will include the specified prompt value, so long as the field hasn't already been set. If it has (on an edit form for example), there's no need to remind the user that they need to select a value so the prompt doesn't appear

like image 84
Gareth Avatar answered Sep 27 '22 17:09

Gareth


I think you can do something like this:

<%= f.select (:sex, %w{ Male Female }, {:include_blank => 'None Specified'} ) %> 
like image 26
jyoseph Avatar answered Sep 27 '22 17:09

jyoseph