views:

1020

answers:

2

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

+5  A: 

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.

Justin French
A: 

Thanks for your answer.

I tried a :as => select before, but then I had trouble sorting the data alphabetically.

It is a many-to-many relationship, but sometimes there's no organization connected to this model. If the user by mistake selected a field in the multi-select, there's no way of undoing it now. That's why I figured we needed a blank, so the user could also explicitly select 'there is none'.

Kind Regards,

Dennis

Dennis
You can provide the :collection option with :as => :select to sort out the ordering issue.
Justin French