Using :collection and :include_blank in Formtastic. How to do it?

Dennis picture Dennis · Jan 21, 2010 · Viewed 10.5k times · Source

I use the superb Formtastic plugin for Ruby on Rails.

Does anybody know how to include a blank (option), when using a custom collection?

When I try:

<%= f.input :organizations, :collection => Organization.all(:order => :name), :include_blank => true %>

I get the select box with the collection, but NOT a blank...

Answer

Justin French picture Justin French · Jan 22, 2010

What kind of association is :organizations? Does it work if you specify :as => :select?

There's spec coverage for the following belongs_to select, date, time and datetime inputs:

f.input(:author, :as => :select, :include_blank => true)
f.input(:created_at, :as => :date, :include_blank => true)
f.input(:created_at, :as => :time, :include_blank => true)
f.input(:created_at, :as => :datetime, :include_blank => true)

My guess is that organizations is not a belongs_to association, right? If it's a :has_many or :has_and_belongs_to_many association, Formtastic will try to do checkboxes or a multi-select. In the case of a multi-select, obviously it makes no sense to have a blank line in there (you just don't select any of the items).

Hope this helps, please post some more details about the models and associations in question.