Skip to content

Instantly share code, notes, and snippets.

@23inhouse
Created September 2, 2012 03:24
Show Gist options
  • Save 23inhouse/3594537 to your computer and use it in GitHub Desktop.
Save 23inhouse/3594537 to your computer and use it in GitHub Desktop.
Simple form mappings

Config

# config/initializers/simple_form.rb

SimpleForm.setup do |config|
  config.wrappers :default,
                  :class => 'input',
                  :hint_class => 'field_with_hint',
                  :error_class => 'field_with_errors' do |b|

    # Determines whether to use HTML5 (:email, :url, ...)
    # and required attributes
    b.use :html5

    # Calculates placeholders automatically from I18n
    # You can also pass a string as f.input :placeholder => "Placeholder"
    b.use :placeholder

    # Calculates maxlength from length validations for string inputs
    b.optional :maxlength

    # Calculates pattern from format validations for string inputs
    b.optional :pattern

    # Calculates min and max from length validations for numeric inputs
    b.optional :min_max

    # Calculates readonly automatically from readonly attributes
    b.optional :readonly

    ## Inputs
    b.use :label_input
    b.use :hint,  :wrap_with => { :tag => :span, :class => 'hint' }
    b.use :error, :wrap_with => { :tag => :span, :class => 'error' }
  end
end

Mappings/Inputs available

SimpleForm comes with a lot of default mappings:

Mapping               Input                         Column Type

boolean               check box                     boolean
string                text field                    string
email                 email field                   string with name matching "email"
url                   url field                     string with name matching "url"
tel                   tel field                     string with name matching "phone"
password              password field                string with name matching "password"
search                search                        -
text                  text area                     text
file                  file field                    string, responding to file methods
hidden                hidden field                  -
integer               number field                  integer
float                 number field                  float
decimal               number field                  decimal
range                 range field                   -
datetime              datetime select               datetime/timestamp
date                  date select                   date
time                  time select                   time
select                collection select             belongs_to/has_many/has_and_belongs_to_many associations
radio_buttons         collection radio buttons      belongs_to
check_boxes           collection check boxes        has_many/has_and_belongs_to_many associations
country               country select                string with name matching "country"
time_zone             time zone select              string with name matching "time_zone"

--boostrap Extras

# config/initializers/simple_form.rb

SimpleForm.setup do |config|

  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |ba|
      ba.use :input
      ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
      ba.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end

  config.wrappers :prepend, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.wrapper :tag => 'div', :class => 'input-prepend' do |prepend|
        prepend.use :input
      end
      input.use :hint,  :wrap_with => { :tag => 'span', :class => 'help-block' }
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
    end
  end

  config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.wrapper :tag => 'div', :class => 'input-append' do |append|
        append.use :input
      end
      input.use :hint,  :wrap_with => { :tag => 'span', :class => 'help-block' }
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
    end
  end

  config.default_wrapper = :bootstrap

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment