# Blank option with select tag

**URL:** <https://www.ruby-forum.com/t/blank-option-with-select-tag/133686>\
**Category:** Rails\
**Created:** [April 2, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686 "2008-04-02T16:27:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [April 2, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686/1 "2008-04-02T16:27:23Z")

</div>

Anybody know how to add a Blank option to a select\_tag. This is what I  
have now

\<%= select\_tag “province”,"  
Prince Edward Island  
New Brunswick  
Nova Scotia  
Newfoundland"  
%\>

This is the output I would like  
All  
Prince  
Edward Island  
New Brunswick\</  
option\>  
Nova Scotia\</  
option\>  
Newfoundland\</  
option\>

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [April 2, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686/2 "2008-04-02T17:20:46Z")

</div>

> Anybody know how to add a Blank option to a select\_tag. This is what I  
> have now

add as an option… :include\_blank =\> true

-philip

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [April 2, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686/3 "2008-04-02T18:36:33Z")

</div>

Thanks but how do I add that into the code. I tried:

\<%= select\_tag “province”,"  
Prince Edward Island  
New Brunswick  
Nova Scotia  
Newfoundland :include\_blank  
=\> true" %\>

I am a newbie so please excuse me.

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [April 3, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686/4 "2008-04-03T16:32:06Z")

</div>

How about something like this:

provinces = ["", “Prince Edward Island”, “New Brunswick”, “Nova  
Scotia”, …]  
province\_collection = provinces.collect{|province| [province,  
province]}  
province\_options = options\_for\_select(province\_collection)

\<%= select\_tag :province, province\_options %\>

The first three lines could/should be in the controller (in which case  
province\_options would need to be an instance variable  
@province\_options). I’ve broken them down just for the sake of  
clarity; you could easily enough chain things together. The first  
line just creates an array of strings of province names and the second  
line creates a new array that will look like this [[’’, ‘’], [‘PEI’,  
‘PEI’], …] options\_for\_select (third line) takes that array and  
converts it into a string of option tags that is suitable for use with  
your select\_tag.

---

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [April 3, 2008, 4:40am UTC](https://www.ruby-forum.com/t/blank-option-with-select-tag/133686/5 "2008-04-03T04:40:37Z")

</div>

Hi Sean,

Why don’t you put those options in, then?

Julian

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO  
(#2) OUT NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
