# select box pre-selecting an option

**URL:** <https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859>\
**Category:** rubyonrails-talk\
**Created:** [May 10, 2012, 10:45am UTC](https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859 "2012-05-10T10:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 10, 2012, 10:45am UTC](https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859/1 "2012-05-10T10:45:36Z")

</div>

Can anyone please tell me what's going on here, I've copied something pretty much directly from rails guides, but it's just not doing what it's supposed to!

(Or I'm doing something stupid!)

rails - 3.2.2 ruby 1.9.3

my view has the following

1 - \<% formations\_array = @formations.map { |f| [f, f] } %\> 2 - \<%= formations\_array.each do |f| f.to\_s end%\> 3 - \<%= f.select(:formation\_csv, options\_for\_select(formations\_array, 2)) %\>

1. the array of arrays is created from the @formations 2. prints this array of arrays [["4-3-3", "4-3-3"], ["5-3-2", "5-3-2"], ["4-4-2", "4-4-2"]] (as I expect)

3. creates the select box, and hard coded asks for index 2 to be currently selected, as on rails docs here : \<%= options\_for\_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %\>

However when I look at the page, the wrong selection is selected, always index 0, not index 2.

Can anyone please tell me what's going on here! Or is this a bug?

Thanks,

Michael

---

<div class="post-metadata">

**Author:** ![Jeremy\_Walker](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jeremy\_Walker](https://discuss.rubyonrails.org/u/Jeremy_Walker)\
**Post date:** [May 10, 2012, 10:57am UTC](https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859/2 "2012-05-10T10:57:09Z")

</div>

> Can anyone please tell me what’s going on here, I’ve copied something
> 
> pretty much directly from rails guides, but it’s just not doing what
> 
> it’s supposed to!
> 
> (Or I’m doing something stupid!)
> 
> rails - 3.2.2
> 
> ruby 1.9.3
> 
> my view has the following
> 
> 1 - \<% formations\_array = @formations.map { |f| [f, f] } %\>
> 
> 2 - \<%= formations\_array.each do |f| f.to\_s end%\>
> 
> 3 - \<%= f.select(:formation\_csv, options\_for\_select(formations\_array,
> 
> 2)) %\>

You are using f.select, rather than select\_tag. This doesn’t require options\_for\_select. It will select the :formation\_csv from the form’s model.

I don’t know what your model is, but let’s say it’s Team.

In your controller:

@team = Team.new

@team.formation\_csv = “5-3-2”

In your view:

\<%= form\_for @team do |f| %\>

\<%= f.select(:number, formations\_array) %\>

\<% end %\>

That will select 5-3-2. If you load the model from the db, it will use the saved value automatically.

Also, I don’t think you need line 1. If you pass an array of values, Rails will use them for both text and value, so line 1 is redundant.

Does that all make sense?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 10, 2012, 11:13am UTC](https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859/3 "2012-05-10T11:13:29Z")

</div>

Thanks for that, it works exactly as you said! cheers, now I have the line

\<%= f.select(:formation\_csv, @formations) %\>

and it magically knows which one to select.

I'm still v curios as to why the "options\_for\_select(formations\_array, 2)" didn't do anything, is it because when I'm using this in conjunction with the form\_helper, ie f.select as opposed to select\_tag, the options\_for\_select method becomes redundant, or different?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Walker](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jeremy\_Walker](https://discuss.rubyonrails.org/u/Jeremy_Walker)\
**Post date:** [May 10, 2012, 11:34am UTC](https://discuss.rubyonrails.org/t/select-box-pre-selecting-an-option/63859/4 "2012-05-10T11:34:00Z")

</div>

> Thanks for that, it works exactly as you said! cheers, now I have the
> 
> line
> 
> \<%= f.select(:formation\_csv, @formations) %\>
> 
> and it magically knows which one to select.
> 
> I’m still v curios as to why the "options\_for\_select(formations\_array,
> 
> 2)" didn’t do anything, is it because when I’m using this in conjunction
> 
> with the form\_helper, ie f.select as opposed to select\_tag, the
> 
> options\_for\_select method becomes redundant, or different?

Yeah, it’s redundant. Your code was also wrong 🙂 The second paramater needs to be the value of the selected object, not the array index. So options\_for\_select(formations\_array, “5-3-2”) would probably have worked. It’s a bad idea though in a resource-based form though.

Glad you’ve got it sorted! My experience with Rails says if something feels like it’s getting unnecessarily complex, you’re probably doing it wrong! 🙂
