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 <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.
[solved] poking through the docs, looks like collection_select will do it -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of JEFF EMMINGER Sent: Thursday, 10 Nov, 2005 4:38 PM To: ''rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org'' Subject: [Rails] generating select lists from collections 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 <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. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails 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.
Collection_select will do it, but it won''t let you use selected_value. This is the problem I''m currently stuck on. On 11/10/05 4:46 PM, "JEFF EMMINGER" <jemminger-m+RvqUOZKbM@public.gmane.org> wrote:> [solved] > poking through the docs, looks like collection_select will do it > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of JEFF EMMINGER > Sent: Thursday, 10 Nov, 2005 4:38 PM > To: ''rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org'' > Subject: [Rails] generating select lists from collections > > > 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 > > <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. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > 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. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 > > <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 & After">Before & After</option> I''d like it to show up like this: <option value="Before & 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: "&".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. -- Posted via http://www.ruby-forum.com/.
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 & After">Before & After</option> > I''d like it to show up like this: > <option value="Before & After">Before & After</option>Actually, I got that backwards, I want it to show up as: <option value="Before & After">Before & 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. -- Posted via http://www.ruby-forum.com/.
ponch wrote:> Collection_select will do it, but it won''t let you use selected_value. > > This is the problem I''m currently stuck on.Check out http://wiki.rubyonrails.org/rails/pages/HowtoUseFormOptionHelpers It has an example showing you how to do that. -- Posted via http://www.ruby-forum.com/.
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: <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? -- Posted via http://www.ruby-forum.com/.