# Options\_for\_select - how to set the selected value?

**URL:** <https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948>\
**Category:** Rails\
**Created:** [May 11, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948 "2007-05-11T21:13:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [May 11, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/1 "2007-05-11T21:13:16Z")

</div>

Ok, this should be simple but I am finding it’s not and previous  
postings don’t seem to shed too much light on this.

I have a select\_tag with 4 values pulled in via a helper:

def filter\_hours  
filter\_hours = [  
[“1 hour”, 1],  
[“24 hours”, 24],  
[“7 days”, 168],  
[“14 days”, 336],  
]  
end

The select\_tag looks like the following:

select\_tag :order\_hours, options\_for\_select(filter\_hours, :selected =\>  
@selected\_order\_hours)

@selected\_order\_hours is being set in the controller when the form is  
posted. It comes back as either 1, 24, 168, 336 and is definitely  
being set as confirmed through logging and printing from the view.

However, the selected valued is never set!

I have tried all combinations of :selected, selected=, etc to no  
avail.

What’s wrong?

Thanks,

Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [May 14, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/2 "2007-05-14T23:28:19Z")

</div>

Anyone have any ideas on this one?

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [May 15, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/3 "2007-05-15T15:16:02Z")

</div>

On 5/11/07, Scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The select\_tag looks like the following:
> 
> select\_tag :order\_hours, options\_for\_select(filter\_hours, :selected =\>  
> @selected\_order\_hours)
> 
> @selected\_order\_hours is being set in the controller when the form is  
> posted. It comes back as either 1, 24, 168, 336 and is definitely  
> being set as confirmed through logging and printing from the view.
> 
> However, the selected valued is never set!

It should just be:

options\_for\_select(filter\_hours, @selected\_order\_hours)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [May 28, 2007, 12:39am UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/4 "2007-05-28T00:39:06Z")

</div>

Tried that … doesn’t work. @selected\_order\_hours is definitely being  
set.

Any other ideas?

Thanks,

Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [May 28, 2007, 12:52am UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/5 "2007-05-28T00:52:10Z")

</div>

Figured it out. Had to change:

select\_tag :order\_hours, options\_for\_select(filter\_hours,  
@selected\_order\_hours)  
to  
select\_tag :order\_hours, options\_for\_select(filter\_hours,  
@selected\_order\_hours.to\_i)

Picky picky!

Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 17, 2011, 9:47pm UTC](https://www.ruby-forum.com/t/options-for-select-how-to-set-the-selected-value/103948/6 "2011-08-17T21:47:16Z")

</div>

Scott wrote in post #251131:

> Figured it out. Had to change:
> 
> select\_tag :order\_hours, options\_for\_select(filter\_hours,  
> @selected\_order\_hours)  
> to  
> select\_tag :order\_hours, options\_for\_select(filter\_hours,  
> @selected\_order\_hours.to\_i)
> 
> Picky picky!
> 
> Scott

was stuck on a similar problem, my solution was

\<%= f.select :grade, ((1…9).to\_a) %\>
