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 doing Company.public.not_broken.all
f.association :company, :scope => [ :public, :not_broken ]
# Same as using :order option, but overriding collection
f.association :company, :collection => Company.all(:order => 'name')
# Company.all(:conditions => { :active => true })
f.association :company, :conditions => { :active => true }
# Company.all(:order => 'name')
f.association :company, :order => 'name'
end
f.association :company # Company.all
simple_form_for @user do |f|
== Examples
* :scope - Given as scopes when retrieving the collection
* :order - Given as order when retrieving the collection
* :joins - Given as joins when retrieving the collection
* :include - Given as include when retrieving the collection
* :conditions - Given as conditions when retrieving the collection
== Options
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) 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 options[:as] ||= :select @reflection = find_association_reflection(association) raise "Association #{association.inspect} not found" unless @reflection case @reflection.macro when :belongs_to attribute = @reflection.options[:foreign_key] || :"#{@reflection.name}_id" when :has_one raise ":has_one association are not supported by f.association" else attribute = :"#{@reflection.name.to_s.singularize}_ids" if options[:as] == :select html_options = options[:input_html] ||= {} html_options[:size] ||= 5 html_options[:multiple] = true unless html_options.key?(:multiple) end end options[:collection] ||= begin finders = options.slice(:conditions, :order, :include, :joins) finders[:conditions] = @reflection.klass.merge_conditions(finders[:conditions], @reflection.options[:conditions]) klass = Array(options[:scope]).inject(@reflection.klass) do |klass, scope| klass.send(scope) end klass.all(finders) end returning(input(attribute, options)) { @reflection = nil } end
def button(type, *args)
your buttons.
be used by SimpleForm, as long as it ends with _tag. So is quite easy to customize
This comes with a bonus that any method added to your ApplicationController can
f.button :image_submit, "/images/foo/bar.png"
And if you want to use image_submit_tag, just give it as an option:
f.button :submit, :confirm => "Are you sure?"
use :confirm normally:
All options given to button are given straight to submit_tag. That said, you can
submit_tag "Create a new user"
above is just calling:
button is actually just a wrapper that adds a default text, that said, f.button
f.button :submit, :label => "Create a new user"
f.button :submit, "Create a new user"
giving a second parameter or giving :label.
otherwise it will create with label "Update User". You can overwrite the label
If the record is a new_record?, it will create a button with label "Create User",
end
f.button :submit
form_for @user do |f|
Creates a button:
def button(type, *args) options = args.extract_options! value = args.first || options.delete(:label) key = @object ? (@object.new_record? ? :create : :update) : :submit value ||= begin model = if @object.class.respond_to?(:human_name) @object.class.human_name else @object_name.to_s.humanize end I18n.t(:"simple_form.buttons.#{key}", :model => model, :default => "#{key.to_s.humanize} #{model}") end options[:class] = "#{key} #{options[:class]}".strip @template.send(:"#{type}_tag", value, options) end
def default_input_type #:nodoc:
collection is given.
default alwayls fallback to the user :as option, or to a :select when a
Attempt to guess the better input type given the defined options. By
def default_input_type #:nodoc: return @options[:as].to_sym if @options[:as] return :select if @options[:collection] input_type = @column.try(:type) case input_type when :timestamp :datetime when :string, nil match = case @attribute_name.to_s when /password/ then :password when /time_zone/ then :time_zone when /country/ then :country end match || input_type || file_method? || :string else input_type end end
def define_simple_form_attributes(attribute_name, options) #:nodoc:
Setup default simple form attributes.
def define_simple_form_attributes(attribute_name, options) #:nodoc: @options = options if @attribute_name = attribute_name @column = find_attribute_column @input_type = default_input_type end end
def error(attribute_name, options={})
f.error :name, :id => "cool_error"
f.error :name
== Examples
contains errors. All the given options are sent as :error_html.
Creates an error tag based on the given attribute, only when the attribute
def error(attribute_name, options={}) define_simple_form_attributes(attribute_name, :error_html => options) SimpleForm::Inputs::Base.new(self).error end
def file_method? #:nodoc:
Checks if attribute is a file_method.
def file_method? #:nodoc: file = @object.send(@attribute_name) if @object.respond_to?(@attribute_name) :file if file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) } end
def find_association_reflection(association) #:nodoc:
Find reflection related to association
def find_association_reflection(association) #:nodoc: @object.class.reflect_on_association(association) if @object.class.respond_to?(:reflect_on_association) end
def find_attribute_column #:nodoc:
Finds the database column for the given attribute
def find_attribute_column #:nodoc: @object.column_for_attribute(@attribute_name) if @object.respond_to?(:column_for_attribute) end
def hint(attribute_name, options={})
f.hint "Don't forget to accept this"
f.hint :name, :id => "cool_hint"
f.hint :name # Do I18n lookup
== Examples
as :hint_html.
an attribute for I18n lookup or a string. All the given options are sent
Creates a hint tag for the given attribute. Accepts a symbol indicating
def hint(attribute_name, options={}) attribute_name, options[:hint] = nil, attribute_name if attribute_name.is_a?(String) define_simple_form_attributes(attribute_name, :hint => options.delete(:hint), :hint_html => options) SimpleForm::Inputs::Base.new(self).hint end
def input(attribute_name, options={}, &block)
given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
Some inputs, as :time_zone and :country accepts a :priority option. If none is
== Priority
:value_method => the method to apply on the array collection to get the value
:label_method => the method to apply on the array collection to get the label
:collection => use to determine the collection to generate the radio or select
options:
When playing with collections (:radio and :select inputs), you have three extra
== Collection
f.input :created_at, :include_blank => true
prompt and/or include blank. Such options are given in plainly:
Some inputs, as datetime, time and select allow you to give extra options, like
== Options
input html, supply :input_html instead and so on.
the label html you just need to give a hash to :label_html. To configure the
Besides the html for any component can be changed. So, if you want to change
:hint => false. The same works for :error, :label and :wrapper.
So, for instance, if you need to disable :hint for a given input, you can pass
The fact SimpleForm is built in components allow the interface to be unified.
by default.
:required => defines whether this attribute is required or not. True
can use it to generate a text field for a date column.
:as => allows you to define the input type you want, for instance you
You have some options for the input to enable/disable some functions:
heuristic to determine which is the best option.
Each database type will render a default input, based on some mappings and
can't be blank
My hint
name="user[name]" size="100" type="text" value="Carlos" />
* Super User Name!
def input(attribute_name, options={}, &block) define_simple_form_attributes(attribute_name, options) if block_given? SimpleForm::Inputs::BlockInput.new(self, block).render else klass = self.class.mappings[input_type] || self.class.const_get(:"#{input_type.to_s.camelize}Input") klass.new(self).render end end
def label(attribute_name, *args)
f.label :name, :id => "cool_label"
f.label :name, :required => false
f.label :name, :label => "Name" # Same as above, but adds required tag
f.label :name, "Name" # Same behavior as Rails, do not add required tag
f.label :name # Do I18n lookup
== Examples
as :label_html.
through the :label option or using i18n. All the given options are sent
Creates a default label tag for the given attribute. You can give a label
def label(attribute_name, *args) return super if args.first.is_a?(String) options = args.extract_options! define_simple_form_attributes(attribute_name, :label => options.delete(:label), :label_html => options, :required => options.delete(:required)) SimpleForm::Inputs::Base.new(self).label end