# Problem with options\_for\_select

**URL:** <https://www.ruby-forum.com/t/problem-with-options-for-select/96420>\
**Category:** Rails\
**Created:** [January 24, 2006, 12:45am UTC](https://www.ruby-forum.com/t/problem-with-options-for-select/96420 "2006-01-24T00:45:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alien8\_Recordings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@Alien8\_Recordings](https://www.ruby-forum.com/u/Alien8_Recordings)\
**Post date:** [January 24, 2006, 12:45am UTC](https://www.ruby-forum.com/t/problem-with-options-for-select/96420/1 "2006-01-24T00:45:22Z")

</div>

Hi All,

I am having a little difficulty using the options\_for\_select forms  
helper method. I have a form for an active record object book, which  
belongs to a publisher. Using the options\_for\_select is working fine  
as far as creating the various options, but it will not output the  
“selected=‘selected’” bit of html needed to identify which publisher  
is associated with a give book in the database. In order to so so I  
am passing it the @book.publishers array, but I suppose I am missing  
something. The docs say that the selected element may be an array of  
values, but I can’t get it to work. Any ideas? My code is below.  
Thanks in advance.

Sean

\<%= options\_for\_select (@publishers.map {|p| [truncate (p.publisher\_name, length = 30, truncate\_string = "..."), p.id]}, @book.publishers )%\>

---

<div class="post-metadata">

**Author:** ![Alien8\_Recordings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@Alien8\_Recordings](https://www.ruby-forum.com/u/Alien8_Recordings)\
**Post date:** [January 24, 2006, 8:53am UTC](https://www.ruby-forum.com/t/problem-with-options-for-select/96420/2 "2006-01-24T08:53:43Z")

</div>

You need to give it an id (or perhaps array of ids), so as your second  
parameter try @book.publishers.map {|p| p.id}  
(I think…)

Michael

Alien8 Recordings wrote:

> Hi All,
> 
> I am having a little difficulty using the options\_for\_select forms  
> helper method. I have a form for an active record object book, which  
> belongs to a publisher. Using the options\_for\_select is working fine  
> as far as creating the various options, but it will not output the  
> “selected=‘selected’” bit of html needed to identify which publisher  
> is associated with a give book in the database. In order to so so I  
> am passing it the @book.publishers array, but I suppose I am missing  
> something. The docs say that the selected element may be an array of  
> values, but I can’t get it to work. Any ideas? My code is below.  
> Thanks in advance.
> 
> Sean
> 
> \<%= options\_for\_select (@publishers.map {|p| [truncate (p.publisher\_name, length = 30, truncate\_string = "..."), p.id]}, @book.publishers )%\>

---

<div class="post-metadata">

**Author:** ![Alien8\_Recordings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@Alien8\_Recordings](https://www.ruby-forum.com/u/Alien8_Recordings)\
**Post date:** [January 24, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/problem-with-options-for-select/96420/3 "2006-01-24T17:19:10Z")

</div>

Hi Michael,

I guess that was quite obvious, but thank you, working like a charm!

Sean
