The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Sunday, April 12, 2009

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

5 comments:

Anonymous said...

Thanks - I was wondering what the difference was...

Julien said...

Thank you!! I was scratching my head to find how i could force "prompt" message in collection_select!

Eric Jones said...

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.

Tarnschaf said...

Thank you!

Boda Zsolt said...

that helped, thx!