# Can't get select helper to populate dropdown w/default value

**URL:** <https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492>\
**Category:** Rails\
**Created:** [January 1, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492 "2006-01-01T18:00:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/1 "2006-01-01T18:00:06Z")

</div>

Guys,

I searched this forum and found plenty of people having this problem,  
but I didn’t see any definitive solution. Usings Rails 1.0.

When I edit an existing record, I can’t get the basic “select” helper to  
display the current value contained in the field of the record. All of  
my other (text) fields populate correctly with current field values.

There appears to be a “:selected =\>” option with the select helper and  
some of its variations, but I can’t get it to work. Frankly, I thought  
it would simply “work”, in the same way that text fields get populated  
correctly.

Am I doing something wrong, is the select helper broken, or is it simply  
not supposed to display the current value when editing?

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/2 "2006-01-01T18:57:33Z")

</div>

Post some code for us to look at. There can be a lot of little things to  
look at in these cases, and its tough to help with out seeing the code.

-Nick

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/3 "2006-01-01T19:10:03Z")

</div>

Peter J. wrote:

> Am I doing something wrong, is the select helper broken, or is it simply  
> not supposed to display the current value when editing?

I suspect you are doing something wrong. If you use the select helpers,  
they will automatically select the correct value for you if you have  
them set up properly.

One thing that could do it is if the values displayed in your dropdown  
do not match the values in your database. For instance, if you are  
displaying usernames, but storing user ids.

If that is the case, make sure you populate the option array properly  
using something like this…

select(“post”, “person\_id”, Person.find\_all.collect {|p| [ p.name, p.id  
] }, { :include\_blank =\> true })

The ‘collect’ being the important part here. The first value is what  
gets displayed, and the second is the internal value used by the select  
dropdown. In this case, the @post.person\_id value should be an ‘id’,  
not a ‘name’.

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/4 "2006-01-01T19:12:17Z")

</div>

Nick,

Here’s the hash that defines the contents of the dropdown:

YESNO = { “No” =\> “0”,  
“Yes” =\> “1” }

In other words, the user sees options for “Yes” and "No, and either 0 or  
1 are stored in the table’s field. In this case, the user can specify  
whether or not they are an administrator.

Here’s my select helper code:

\<%= select ‘user’, ‘administrator’, User::YESNO.sort, :include\_blank =\>  
TRUE %\>

I tried all manner of including :selected =\> ??? and it’s never worked.

Now, I have found the following workaround, which solves the problem by  
using Rails’ options\_for\_select helper:

Administrator  
 \<%= options\_for\_select User::YESNO.sort, @user.administrator.to\_s %\>

But going back to the original question, shouldn’t the basic select  
helper be able to handle this?

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/5 "2006-01-01T19:33:17Z")

</div>

Guys,

Ok, problem solved. I changed my hash from:

YESNO = { “No” =\> “0”,  
“Yes” =\> “1” }

to:

YESNO = { “No” =\> 0,  
“Yes” =\> 1 }

The field’s type in my table is SMALLINT, so now the hash matches.  
So…the following code works:

\<%= select ‘user’, ‘administrator’, Bug::YESNO, :include\_blank =\> TRUE  
%\>

Thanks Kevin & Nick! Happy New Year!

Hope this helps the other folks getting this kind of thing to work!

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/6 "2006-01-01T19:27:47Z")

</div>

Kevin,

I understand what you said, but I don’t know if I need to use the  
.collect the way you did, nor can I get it to work anyway. Since the  
data source for my dropdown is a hash and not the contents of a lookup  
table, and the dropdown itself is being populated correctly, I’m not  
sure if you your technique fits my situation. Suggestions?

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 2, 2006, 7:09am UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/7 "2006-01-02T07:09:28Z")

</div>

Nick S. wrote:

> Well thats the way I do it Peter, using the options helper that is. By  
> looking at the docs for the select helper, it doesn’t need/have a  
> selected  
> value, it should automatically pick the right one. Like I said, I  
> usually  
> always use the options helper and never really had any problems with  
> that.
> 
> -Nick

incidentally, if you try to use the :prompt option for the select  
helpers, make sure that your database table can accept a null for that  
value. The prompt is only selected by default when the corresponding  
model value is a null.

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [January 1, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/cant-get-select-helper-to-populate-dropdown-w-default-value/51492/8 "2006-01-01T19:33:39Z")

</div>

Well thats the way I do it Peter, using the options helper that is. By  
looking at the docs for the select helper, it doesn’t need/have a  
selected  
value, it should automatically pick the right one. Like I said, I  
usually  
always use the options helper and never really had any problems with  
that.

-Nick
