Inserting a nil value into a Select/Drop Down Menu


#1

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!


#2

Since options_from_collection_for_select returns a string of normal

tags, you should be able to just append it to your own tag:

<%= select_tag “cuisine_id”,
“All Cuisines” +
options_from_collection_for_select(@cuisines, ‘id’, ‘name’) %>

Or make a helper that does this for you.

Justin


#3

blaix wrote:

Since options_from_collection_for_select returns a string of normal

tags, you should be able to just append it to your own tag:

<%= select_tag “cuisine_id”,
“All Cuisines” +
options_from_collection_for_select(@cuisines, ‘id’, ‘name’) %>

Or make a helper that does this for you.

Justin

Thanks this works perfectly!


#4

Rich,

Does this code still work if you have a form validation error? Where
are you declaring the @cuisines object?


#5

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 removed_email_address@domain.invalid 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