# Inserting a nil value into a Select/Drop Down Menu

**URL:** <https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231>\
**Category:** Rails\
**Created:** [December 22, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231 "2006-12-22T16:17:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 22, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231/1 "2006-12-22T16:17:42Z")

</div>

I might have asked this before, but I don’t think I got a reply.

I’m using this code to create a drop-down menu of ‘cuisines’:

\<%= select\_tag “cuisine\_id”,  
options\_from\_collection\_for\_select(@cuisines, ‘id’, ‘name’) %\>

My question is, how can I add an “All Cuisines” option to the start of  
this menu, with a nil value? Or is there someway that lets you add a  
similar option? It’s a common thing to have in a drop-down menu so I’m  
sure it’s been discussed somewhere.

I’m guessing I’d probably have to code it more explicitly. Thanks!

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 23, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231/2 "2006-12-23T23:11:59Z")

</div>

Since options\_from\_collection\_for\_select returns a string of normal

 tags, you should be able to just append it to your own tag:

\<%= select\_tag “cuisine\_id”,  
“All Cuisines” +  
options\_from\_collection\_for\_select(@cuisines, ‘id’, ‘name’) %\>

Or make a helper that does this for you.

Justin

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 27, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231/3 "2006-12-27T23:17:16Z")

</div>

blaix wrote:

> Since options\_from\_collection\_for\_select returns a string of normal
> 
> tags, you should be able to just append it to your own tag:
> 
> \<%= select\_tag “cuisine\_id”,  
> “All Cuisines” +  
> options\_from\_collection\_for\_select(@cuisines, ‘id’, ‘name’) %\>
> 
> Or make a helper that does this for you.
> 
> Justin

Thanks this works perfectly!

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2006, 6:07am UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231/4 "2006-12-28T06:07:02Z")

</div>

Rich,

Does this code still work if you have a form validation error? Where  
are you declaring the @cuisines object?

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2006, 6:51am UTC](https://www.ruby-forum.com/t/inserting-a-nil-value-into-a-select-drop-down-menu/80231/5 "2006-12-28T06:51:10Z")

</div>

The option way works, but I do it this way as well for a list of  
countries  
where I want USA to appear first…

@usa = Country.find(:first, :conditions =\> [“name = ?”, usa\_name])  
@countries = Country.find(:all,  
:conditions =\> [‘country\_code IS NOT NULL  
AND  
name \<\> ?’, usa\_name],  
:order =\> ‘name ASC’)  
@countries.insert(0, @usa)

You might want to do a similar thing in your controller, who knows.

On 12/27/06, JL Smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rich,
> 
> Does this code still work if you have a form validation error? Where  
> are you declaring the @cuisines object?
> 
> >

## –

## seth at subimage interactive

## [http://www.subimage.com](http://www.subimage.com) [http://sublog.subimage.com](http://sublog.subimage.com)

> **[Freelance Time Tracking Software — Online Invoice Software](https://cashboardapp.com/)**
>
> Freelance time tracking software, online invoice software & project management.
> Used by thousands worldwide since 2007. FREE trial.

[http://dev.subimage.com/projects/substruct](http://dev.subimage.com/projects/substruct)
