How to include a "Please select..." (default/prompt) in a grouped dropdown list?
Asked Answered
M

1

5

The code I am using for the dropdown list is this:

<%= f.select :post_type_id, option_groups_from_collection_for_select(@categories, :post_types, :name, :id, :name) %>

It neatly divides the options into optgroups.

But how do I modify the code to include a prompt (or a default value) of "Please select..." ?

It seems hard to do with grouped dropdowns. (The rails docs seem to suggest using a hash, but I've tried several alternatives without success.)

Metagalaxy answered 5/1, 2012 at 12:54 Comment(0)
M
7

Bah, right after I posted the question I found the answer was in the docs for select, and not under option_groups_from_collection_for_selectdocs where I had been looking.

The answer is:

<%= f.select :post_type_id, option_groups_from_collection_for_select(@categories, :post_types, :name, :id, :name), :include_blank => "Please select..." %>
Metagalaxy answered 5/1, 2012 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.