# Collection\_select not selecting default value

**URL:** <https://www.ruby-forum.com/t/collection-select-not-selecting-default-value/157762>\
**Category:** Rails\
**Created:** [January 15, 2009, 2:37am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-default-value/157762 "2009-01-15T02:37:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2009, 2:37am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-default-value/157762/1 "2009-01-15T02:37:28Z")

</div>

Hi,

I’m trying to create a dropdown using collection\_select. However, I cant  
seem to get it to select the right option by default.

In my controller I have a setup like this:

@content = Content.new  
@content.selection\_id = 3  
@selections = Selection.find(:all)

In my view I have:

\<% form\_for :content, @content, :url =\> { :action =\> ‘create’ } do |f|  
%\>  
\<%= f.collection\_select :selection, @selections, :id, :name %\>  
\<%= submit\_tag ‘Create’ %\>  
\<% end %\>

Despite me specifying the default value in the controller,  
collection\_select doesnt seem to pick that up when rendering the html,  
which defaults to the first option in the dropdown.

What idiot-simple thing am I missing? 😉

Thanks!

g.

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2009, 4:17am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-default-value/157762/2 "2009-01-15T04:17:19Z")

</div>

On Wed, Jan 14, 2009 at 8:37 PM, Gu stav  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @selections = Selection.find(:all)
> 
> In my view I have:
> 
> \<% form\_for :content, @content, :url =\> { :action =\> ‘create’ } do |f|  
> %\>  
> \<%= f.collection\_select :selection, @selections, :id, :name %\>  
> \<%= submit\_tag ‘Create’ %\>  
> \<% end %\>

The first thing that sticks out is it should be :selection\_id instead  
of :selection.

> Despite me specifying the default value in the controller,  
> collection\_select doesnt seem to pick that up when rendering the html,  
> which defaults to the first option in the dropdown.
> 
> What idiot-simple thing am I missing? 😉

And if that doesn’t fix it, then my only other suggestion would be to  
make sure you have selection\_id set as an integer in the database.  
I’ve accidentally created a foreign key as a string before, which  
works fine mostly, except when trying to do a select box and it’s  
comparing integers to strings.

Hope that helps.

## – Brandon

Training by Collective Idea: Ruby on Rails training in a vacation  
setting  
[http://training.collectiveidea.com](http://training.collectiveidea.com) – San Antonio, TX – Jan 20-23

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [January 15, 2009, 5:18am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-default-value/157762/3 "2009-01-15T05:18:42Z")

</div>

> The first thing that sticks out is it should be :selection\_id instead  
> of :selection.

that fixes it. thanks a million!
