house9

random code and what not

rails collection_select to prompt or include_blank?

Well I just wasted a bit of time but learned something in the end. When working with the rails form helper collection_select you might be interested in the difference between the prompt option and include_blank? option

include_blank will always create an option tag, prompt will only create an option tag when creating a new record - in most cases I prefer include_blank

both take either a boolean or a string, if true is passed then include_blank will create an option with no display text and prompt defaults to the display text of ‘Please Select’
collection_select(:product,
:category_id,
Category.all,
:id,
:title,
{:prompt => true}
)

collection_select(:product,
:category_id,
Category.all,
:id,
:title,
{:include_blank => 'Please Select'}
)
both of these result in the same html, but the first one will not include the ‘Please Select’ option when you return to edit the previously created Product

See also

Comments

Tarnschaf
Thank you!
Eric Jones
Thank you! I was trying to figure out why I couldn't get the prompt to show up, now I know why. I was editing an existing record.
Julien
Thank you!! I was scratching my head to find how i could force "prompt" message in collection_select!
danieltsadok
Thanks - I was wondering what the difference was…