# List Box Issue - Should be easy, but nor for me!

**URL:** <https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255>\
**Category:** Rails\
**Created:** [December 15, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255 "2007-12-15T14:37:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tedster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@tedster](https://www.ruby-forum.com/u/tedster)\
**Post date:** [December 15, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255/1 "2007-12-15T14:37:33Z")

</div>

I have the following snippet in my app:

\<% @anitem = Item.find(:all, :conditions =\> "id = " + params[:id]) %\>  
\<% form\_for :anitem, :url =\> { :action =\> :updateitem } do |thisform|  
-%\>  
…  
… (some code…removed)  
…  
\<%= collection\_select :thisform, :item\_category,  
ItemCategory.find(:all), :id, :name %\>  
  
\<%= collection\_select :thisform, :packing\_list\_type,  
PackingListType.find(:all), :id, :name %\>  
\<%= submit\_tag ‘Update’ %\>  
\<% end %\>

My issue is that the list box never selects the current value when it  
pulls the data from the database.

Everything looks great - but no currently selected option tag  
(“selected”).

Should be easy, but I can’t get it to work.

Any ideas?

---

<div class="post-metadata">

**Author:** ![tedster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@tedster](https://www.ruby-forum.com/u/tedster)\
**Post date:** [December 15, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255/2 "2007-12-15T17:16:05Z")

</div>

On Dec 15, 1:37 pm, Ted S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= collection\_select :thisform, :packing\_list\_type,  
> PackingListType.find(:all), :id, :name %\>  
> \<%= submit\_tag ‘Update’ %\>  
> \<% end %\>

When using the form\_for stuff you need to do  
thisform.collection\_select :field\_name,…

Fred

---

<div class="post-metadata">

**Author:** ![tedster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@tedster](https://www.ruby-forum.com/u/tedster)\
**Post date:** [December 16, 2007, 2:04am UTC](https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255/3 "2007-12-16T02:04:34Z")

</div>

Well the documentation shows an example of how to select an element:

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000928)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

Do a view source and see if there is “selected” for an option. Put a  
debug  
statement and look at the log file to check if there is any value that  
is  
returned by the db.

---

<div class="post-metadata">

**Author:** ![tedster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@tedster](https://www.ruby-forum.com/u/tedster)\
**Post date:** [December 16, 2007, 4:43am UTC](https://www.ruby-forum.com/t/list-box-issue-should-be-easy-but-nor-for-me/123255/4 "2007-12-16T04:43:35Z")

</div>

> When using the form\_for stuff you need to do  
> thisform.collection\_select :field\_name,…

Thanks, Fred. Couldn’t get that to work. I tried various permutations.

I am using this, and it works, but it’s a hack of sorts:

\<%= select(:aform, “packing\_list\_type\_id”,  
PackingListType.find(:all).collect {|p| [p.name, p.id] }, {:selected  
=\> @anitem[0][‘packing\_list\_type\_id’]}) %\>

It uses the :selected thingy to make it all work.

- tedstur
