Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 952
Dennis Avatar asked Jan 21 '10 15:01

Dennis


1 Answers

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.

like image 100
Justin French Avatar answered Nov 11 '22 12:11

Justin French