Generating select lists from collections


#1

working through “four days on rails”
(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.


#2

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 ), 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.


#3

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.


#4

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 ), 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?