Method: SimpleForm::FormBuilder#input

Defined in:

#input(attribute_name, options = {}, &block) ⇒ Object Also known as: attribute

Basic input helper, combines all components in the stack to generate input html based on options the user define and some guesses through database column information. By default a call to input will generate label + input + hint (when defined) + errors (when exists), and all can be configured inside a wrapper html.

If a block is given, the contents of the block will replace the input field that would otherwise be generated automatically. The content will be given a label and wrapper div to make it consistent with the other elements in the form.


# Imagine @user has error "can't be blank" on name
simple_form_for @user do |f|
  f.input :name, hint: 'My hint'

This is the output html (only the input portion, not the form):

<label class="string required" for="user_name">
  <abbr title="required">*</abbr> Super User Name!
<input class="string required" id="user_name" maxlength="100"
   name="user[name]" type="text" value="Carlos" />
<span class="hint">My hint</span>
<span class="error">can't be blank</span>

Each database type will render a default input, based on some mappings and heuristic to determine which is the best option.

You have some options for the input to enable/disable some functions:

as: allows you to define the input type you want, for instance you
       can use it to generate a text field for a date column.

required: defines whether this attribute is required or not. True
            by default.

The fact SimpleForm is built in components allow the interface to be unified. So, for instance, if you need to disable :hint for a given input, you can pass hint: false. The same works for :error, :label and :wrapper.

Besides the html for any component can be changed. So, if you want to change the label html you just need to give a hash to :label_html. To configure the input html, supply :input_html instead and so on.


Some inputs, as datetime, time and select allow you to give extra options, like prompt and/or include blank. Such options are given in plainly:

f.input :created_at, include_blank: true


When playing with collections (:radio_buttons, :check_boxes and :select inputs), you have three extra options:

collection: use to determine the collection to generate the radio or select

label_method: the method to apply on the array collection to get the label

value_method: the method to apply on the array collection to get the value


Some inputs, as :time_zone and :country accepts a :priority option. If none is given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectively.

# File 'lib/simple_form/form_builder.rb', line 117

def input(attribute_name, options = {}, &block)
  options = @defaults.deep_dup.deep_merge(options) if @defaults

  input   = find_input(attribute_name, options, &block)
  wrapper = find_wrapper(input.input_type, options)

  wrapper.render input