class SimpleForm::FormBuilder
def association(association, options={}, &block)
From the options above, only :collection can also be supplied.
end
c.input :type
c.input :name
f.association :company do |c|
simple_fields_for:
When a block is given, association simple behaves as a proxy to
== Block
# Same as using :order option, but overriding collection
f.association :company, :collection => Company.all(:order => 'name')
end
f.association :company # Company.all
simple_form_for @user do |f|
== Examples
can also be given:
supported in input are also supported by association. Some extra options
collection automatically. It's just a wrapper to input, so all options
Helper for dealing with association selects/radios, generating the
def association(association, options={}, &block) options = options.dup return simple_fields_for(*[association, options.delete(:collection), options].compact, &block) if block_given? raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object reflection = find_association_reflection(association) raise "Association #{association.inspect} not found" unless reflection options[:as] ||= :select options[:collection] ||= reflection.klass.all(reflection.options.slice(:conditions, :order)) attribute = case reflection.macro when :belongs_to (reflection.respond_to?(:options) && reflection.options[:foreign_key]) || :"#{reflection.name}_id" when :has_one raise ":has_one associations are not supported by f.association" else if options[:as] == :select html_options = options[:input_html] ||= {} html_options[:size] ||= 5 html_options[:multiple] = true unless html_options.key?(:multiple) end # Force the association to be preloaded for performance. if options[:preload] != false && object.respond_to?(association) target = object.send(association) target.to_a if target.respond_to?(:to_a) end :"#{reflection.name.to_s.singularize}_ids" end input(attribute, options.merge(:reflection => reflection)) end