# The selected element in a list

**URL:** <https://www.ruby-forum.com/t/the-selected-element-in-a-list/83769>\
**Category:** Rails\
**Created:** [January 29, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/the-selected-element-in-a-list/83769 "2007-01-29T17:06:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 29, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/the-selected-element-in-a-list/83769/1 "2007-01-29T17:06:25Z")

</div>

Hi,  
I’m trying to populate a drop-down list with objects and to set the  
selected object.

\<%= select\_tag ‘list’,  
options\_from\_collection\_for\_select(@menulist,“id”,“name”,  
lamenu.menuelement\_id.to\_s) %\>

lamenu(it has properties “name”,“id” ) is an element from a list  
@menulist.

the output in html my list looks like

\<select id=“list” name=“list”  
…

colour company country county ............. The problem is that i don't have the specified element(lamenu.menuelement\_id.to\_s) in the list selected at the view.

I look in the html ant there’s no with the selected atribute.  
For testing I printed lamenu.menuelement\_id.to\_s near the select box,  
if i put that value explicitly in options\_from\_collection\_for\_select  
example:

\<%= select\_tag ‘list’,  
options\_from\_collection\_for\_select(@menulist,“id”,“name”, 20) %\> , then  
my selected element will be the one with the id 20 and not the first one  
in the list.  
There’s something missing but i don’t kow what…  
Thanks.

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 29, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/the-selected-element-in-a-list/83769/2 "2007-01-29T17:15:26Z")

</div>

🙂 horray, it worked…  
i had to put amenu.menuelement\_id.to\_i for this to work. In my table  
menuelement\_id is an int(11), I think it’s odd to convert it again to  
integer.
