# Collection\_select and select\_tag help

**URL:** <https://www.ruby-forum.com/t/collection-select-and-select-tag-help/156739>\
**Category:** Rails\
**Created:** [January 2, 2009, 9:04am UTC](https://www.ruby-forum.com/t/collection-select-and-select-tag-help/156739 "2009-01-02T09:04:30Z")\
**Posts on this page:** 3\
**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:** [January 2, 2009, 9:04am UTC](https://www.ruby-forum.com/t/collection-select-and-select-tag-help/156739/1 "2009-01-02T09:04:30Z")

</div>

I have created the following select box which works fine except for the  
fact if you edit the item it defaults back to N/A instead of selecting  
the correct location.

select\_tag(“item[location\_id]”, ‘N/A’ +  
options\_for\_select(@locations.collect{|location| [location.name,  
location.id]}))

Ideally, I want to do something like this (which selects the correct  
value on edit):

f.collection\_select(:locations, @locations, :id, :name) %\>

but how can I add an extra blank option at the top like I did with  
select\_tag?

thanks!

---

<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:** [January 2, 2009, 11:14am UTC](https://www.ruby-forum.com/t/collection-select-and-select-tag-help/156739/2 "2009-01-02T11:14:55Z")

</div>

On 2 Jan 2009, at 08:04, Scott K. wrote:

> I have created the following select box which works fine except for  
> the  
> fact if you edit the item it defaults back to N/A instead of selecting  
> the correct location.
> 
> select\_tag(“item[location\_id]”, ‘N/A’ +  
> options\_for\_select(@locations.collect{|location| [location.name,  
> location.id]}))

The (optional) last argument to options\_for\_select is the value to be  
preselected (ie something like item.location\_id)

Fred

---

<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:** [January 2, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/collection-select-and-select-tag-help/156739/3 "2009-01-02T15:15:01Z")

</div>

> > I have created the following select box which works fine except for  
> > the  
> > fact if you edit the item it defaults back to N/A instead of selecting  
> > the correct location.
> > 
> > select\_tag(“item[location\_id]”, ‘N/A’ +  
> > options\_for\_select(@locations.collect{|location| [location.name,  
> > location.id]}))
> 
> The (optional) last argument to options\_for\_select is the value to be  
> preselected (ie something like item.location\_id)
> 
> Fred

ah didn’t see that that. Thanks! i got it working.
