简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM