# Generating select lists from collections

**URL:** <https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648 "2005-11-12T12:49:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648/1 "2005-11-12T12:49:51Z")

</div>

working through “four days on rails”  
([http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf)  
[http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf) ), day three  
has  
me building a select list like so

 \<%= options\_from\_collection\_for\_select @categories, "id", "category", @item.category.id %\>

this smells a little funny to me, as I have to make sure to name the  
field  
in the rails convention myself, whereas using “select” or “select\_tag”  
rails  
will do it for me.

so I tried

\<%= select “item”, “category\_id”, (options\_from\_collection\_for\_select  
@categories, “id”, “category”, @item.category.id) %\>

but since options\_from\_collection\_for\_select escapes html, I get a  
botched  
list of options. is there a better existing way to build a select using  
a  
collection, or do I need to roll my own?

thanks,  
jeff

CONFIDENTIAL NOTICE: This email including any attachments, contains  
confidential information belonging to the sender. It may also be  
privileged or otherwise protected by work product immunity or other  
legal rules. This information is intended only for the use of the  
individual or entity named above. If you are not the intended  
recipient, you are hereby notified that any disclosure, copying,  
distribution or the taking of any action in reliance on the contents  
of this emailed information is strictly prohibited. If you have  
received this email in error, please immediately notify us by  
reply email of the error and then delete this email immediately.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [December 28, 2005, 5:56am UTC](https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648/2 "2005-12-28T05:56:23Z")

</div>

jemminger wrote:

> working through “four days on rails”  
> ([http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf)  
> [http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf) ), day three  
> has  
> me building a select list like so
> 
> \<%= options\_from\_collection\_for\_select @categories, "id", "category", @item.category.id %\>
> 
> this smells a little funny to me, as I have to make sure to name the  
> field  
> in the rails convention myself, whereas using “select” or “select\_tag”  
> rails  
> will do it for me.
> 
> so I tried
> 
> \<%= select “item”, “category\_id”, (options\_from\_collection\_for\_select  
> @categories, “id”, “category”, @item.category.id) %\>
> 
> but since options\_from\_collection\_for\_select escapes html, I get a  
> botched  
> list of options. is there a better existing way to build a select using  
> a  
> collection, or do I need to roll my own?

Did you (or anyone else) ever figure this out? I am mostly concerned  
with the escaping issue. I have an item called “Before & After” that  
shows up like this:

Before & After I'd like it to show up like this: Before & After If that doesn't break things.

Or, is there a routine that renders html entities that I can call in the  
controller? Some method such that:  
“&”.x gives “&”?

Sure, I could write one, but there are a lot of HTML entities out there  
and I don’t want to miss one, or reinvent the wheel.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [December 28, 2005, 6:15am UTC](https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648/3 "2005-12-28T06:15:13Z")

</div>

Dan T. wrote:

> Did you (or anyone else) ever figure this out? I am mostly concerned  
> with the escaping issue. I have an item called “Before & After” that  
> shows up like this:
> 
> Before & After I'd like it to show up like this: Before & After

Actually, I got that backwards,  
I want it to show up as:

Before & After

And I got it to work by changing this line:  
\<%= options\_for\_select n%\>

to this:

\<%= h n %\>

Duh.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 17, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/generating-select-lists-from-collections/47648/4 "2006-05-17T19:27:39Z")

</div>

jemminger wrote:

> working through “four days on rails”  
> ([http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf)  
> [http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf](http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf) ), day three  
> has  
> me building a select list like so

Mine looks like:

**Category:** \<%= options\_from\_collection\_for\_select @categories, "id", "category", @item.category\_id %\> 

but I get an error:

NoMethodError in Items#create

Showing app/views/items/\_form.rhtml where line #13 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.inject

Extracted source (around line #13):

10:   
11: **Category:**   
12:   
13: \<%= options\_from\_collection\_for\_select @categories, “id”,  
“category”, @item.category\_id %\>  
14:   
15:   
16:

As far as I can tell it’s correct. I’m pulling out my hair… any help?
