Forum: Ruby on Rails generating select lists from collections

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
jemminger (Guest)
on 2005-11-12 12:49
(Received via mailing list)
working through "four days on rails"
(http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf
<http://www.rails4days.pwp.blueyonder.co.uk/Rails4D... ), day three
has
me building a select list like so

<select id="item_category_id" name="item[category_id]">
	<%= options_from_collection_for_select @categories, "id",
"category", @item.category.id %>
</select>

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.
A075c500551c200f5d040cf5e7ac254a?d=identicon&s=25 Dan T. (dandante)
on 2005-12-28 05:56
jemminger wrote:
> working through "four days on rails"
> (http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf
> <http://www.rails4days.pwp.blueyonder.co.uk/Rails4D... ), day three
> has
> me building a select list like so
>
> <select id="item_category_id" name="item[category_id]">
> 	<%= options_from_collection_for_select @categories, "id",
> "category", @item.category.id %>
> </select>
>
> 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:
<option value="Before &amp; After">Before &amp; After</option>
I'd like it to show up like this:
<option value="Before &amp; After">Before & After</option>
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:
"&amp;".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.
A075c500551c200f5d040cf5e7ac254a?d=identicon&s=25 Dan T. (dandante)
on 2005-12-28 06:15
Dan Tenenbaum 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:
> <option value="Before &amp; After">Before &amp; After</option>
> I'd like it to show up like this:
> <option value="Before &amp; After">Before & After</option>


Actually, I got that backwards,
I want it to show up as:
<option value="Before & After">Before &amp; After</option>

And I got it to work by changing this line:
<%= options_for_select n%>

to this:
<option value="<%=n%>"><%= h n %></option>

Duh.
0d5576e29cd7b49709c93ae6044a3736?d=identicon&s=25 Ben R. (cantinflas83)
on 2006-05-17 19:27
jemminger wrote:
> working through "four days on rails"
> (http://www.rails4days.pwp.blueyonder.co.uk/Rails4Days.pdf
> <http://www.rails4days.pwp.blueyonder.co.uk/Rails4D... ), day three
> has
> me building a select list like so
>

Mine looks like:

  <tr>
    <td><b>Category: </b></td>
    <td><select id="item_category_id" name="item[category_id]">
      <%= options_from_collection_for_select @categories, "id",
"category", @item.category_id %>
          </select>
    </td>
  </tr>

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:   <tr>
11:     <td><b>Category: </b></td>
12:     <td><select id="item_category_id" name="item[category_id]">
13:       <%= options_from_collection_for_select @categories, "id",
"category", @item.category_id %>
14:           </select>
15:     </td>
16:   </tr>

As far as I can tell it's correct.  I'm pulling out my hair... any help?
This topic is locked and can not be replied to.