I might have asked this before, but I don''t think I got a reply. I''m using this code to create a drop-down menu of ''cuisines'': <%= select_tag "cuisine_id", options_from_collection_for_select(@cuisines, ''id'', ''name'') %> My question is, how can I add an "All Cuisines" option to the start of this menu, with a nil value? Or is there someway that lets you add a similar option? It''s a common thing to have in a drop-down menu so I''m sure it''s been discussed somewhere. I''m guessing I''d probably have to code it more explicitly. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Since options_from_collection_for_select returns a string of normal <option> tags, you should be able to just append it to your own <option> tag: <%= select_tag "cuisine_id", "<option value=''''>All Cuisines</option>" + options_from_collection_for_select(@cuisines, ''id'', ''name'') %> Or make a helper that does this for you. Justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
blaix wrote:> Since options_from_collection_for_select returns a string of normal > <option> tags, you should be able to just append it to your own > <option> tag: > > <%= select_tag "cuisine_id", > "<option value=''''>All Cuisines</option>" + > options_from_collection_for_select(@cuisines, ''id'', ''name'') %> > > Or make a helper that does this for you. > > JustinThanks this works perfectly! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rich, Does this code still work if you have a form validation error? Where are you declaring the @cuisines object? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
subimage interactive
2006-Dec-28 05:49 UTC
Re: Inserting a nil value into a Select/Drop Down Menu
The option way works, but I do it this way as well for a list of countries
where I want USA to appear first...
@usa = Country.find(:first, :conditions => ["name = ?", usa_name])
@countries = Country.find(:all,
:conditions => [''country_code IS NOT
NULL AND
name <> ?'', usa_name],
:order => ''name ASC'')
@countries.insert(0, @usa)
You might want to do a similar thing in your controller, who knows.
On 12/27/06, JL Smith <autiger02-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> Rich,
>
> Does this code still work if you have a form validation error? Where
> are you declaring the @cuisines object?
>
>
> >
>
--
--------------------
seth at subimage interactive
-----
http://www.subimage.com
http://sublog.subimage.com
-----
http://www.getcashboard.com
http://dev.subimage.com/projects/substruct
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---