# Selecting default value in a collection select

**URL:** <https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835>\
**Category:** Rails\
**Created:** [June 19, 2008, 2:11am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835 "2008-06-19T02:11:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 2:11am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835/1 "2008-06-19T02:11:41Z")

</div>

I’m having trouble selecting a default value in my collection select.

here is my code:

f.collection\_select(:item\_type\_id, @item\_types, :id, :name)

i want to select item\_type.name = “N/A” for the default.

any ideas?

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 2:54am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835/2 "2008-06-19T02:54:07Z")

</div>

> f.collection\_select(:item\_type\_id, @item\_types, :id, :name)
> 
> i want to select item\_type.name = “N/A” for the default.

The f…:item\_type\_id pattern will set the collection to whatever the  
target  
of the form\_for has.

I think you don’t want item\_type\_id to come back nil if the user selects  
nothing. That calls for a :prompt =\> ‘N/A’ around the end

Warning: If the form\_for points to an item with a filled-out  
item\_type\_id,  
even if @item\_types has no such item, you won’t get a prompt.

Design Warning: Working with nils is evil. I would use a migration to  
add a  
real ‘N/A’ to the item types, and I would park all unassigned  
item\_type\_id’s  
at it. That allows users to easily reset an item type back to ‘N/A’  
without  
you futzing around extra to provide a prompt. And without excessive nil  
checks in all your item type code.

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 3:21am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835/3 "2008-06-19T03:21:43Z")

</div>

> The f…:item\_type\_id pattern will set the collection to whatever the  
> target of the form\_for has.

Geez - before my aphasia derails anyone…

The f…:item\_type\_id pattern will select the option matching whatever  
the  
target of the form\_for has.

> I think you don’t want item\_type\_id to come back nil if the user selects  
> nothing. That calls for a :prompt =\> ‘N/A’ around the end

I think you want item\_type\_id to come back nil if the user selects  
nothing.  
That calls for a :prompt =\> ‘N/A’ around the end of the  
collection\_select.

Remaining post _might_ be okay… 😉

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 3:26am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835/4 "2008-06-19T03:26:47Z")

</div>

Phlip wrote:

> > f.collection\_select(:item\_type\_id, @item\_types, :id, :name)
> > 
> > i want to select item\_type.name = “N/A” for the default.
> 
> The f…:item\_type\_id pattern will set the collection to whatever the  
> target  
> of the form\_for has.
> 
> I think you don’t want item\_type\_id to come back nil if the user selects  
> nothing. That calls for a :prompt =\> ‘N/A’ around the end
> 
> Warning: If the form\_for points to an item with a filled-out  
> item\_type\_id,  
> even if @item\_types has no such item, you won’t get a prompt.
> 
> Design Warning: Working with nils is evil. I would use a migration to  
> add a  
> real ‘N/A’ to the item types, and I would park all unassigned  
> item\_type\_id’s  
> at it. That allows users to easily reset an item type back to ‘N/A’  
> without  
> you futzing around extra to provide a prompt. And without excessive nil  
> checks in all your item type code.

thanks for the reply! i just had to set

@item.item\_type\_id = “1”

in my new method in the controller. woohoo! i can finish coding now!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 19, 2008, 7:33am UTC](https://www.ruby-forum.com/t/selecting-default-value-in-a-collection-select/140835/5 "2008-06-19T07:33:11Z")

</div>

Scott K. wrote:

> I’m having trouble selecting a default value in my collection select.
> 
> here is my code:
> 
> f.collection\_select(:item\_type\_id, @item\_types, :id, :name)
> 
> i want to select item\_type.name = “N/A” for the default.
> 
> any ideas?

Use Like

f.collection\_select(:item\_type\_id, @item\_types, :id,  
:name,:include\_blank=\>“N/A”)

it will pass null value to controller and in front end page also “N/A”  
will display
