Skip to content

Instantly share code, notes, and snippets.

@gonzalo-bulnes
Last active December 21, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzalo-bulnes/6246474 to your computer and use it in GitHub Desktop.
Save gonzalo-bulnes/6246474 to your computer and use it in GitHub Desktop.
A sweet transition from Bootstrap 2.3 to Bootstrap 3 using a Simple Form custom form builder.
# lib/custom_form_builder.rb
# See https://github.com/plataformatec/simple_form#custom-form-builder
class CustomFormBuilder < SimpleForm::FormBuilder
def input(attribute_name, options = {}, &block)
if options[:wrapper_html]
options[:wrapper_html].merge! class: 'form-group'
else
options[:wrapper_html] = { class: 'form-group' }
end
# inline checkboxes should not receive the .form-control class
unless [:remember_me].include? attribute_name
if options[:input_html]
options[:input_html].merge! class: 'form-control'
else
options[:input_html] = { class: 'form-control' }
end
end
if options[:hint_html]
options[:error_html].merge! class: 'help-block'
else
options[:error_html] = { class: 'help-block' }
end
super
end
end
# app/views/devise/sessions/new.html.haml
# This example uses a standard Devise view to illustrate how the cstom_form_builder can be used
# to generate a Bootstrap 2.3 AND Bootstrap 3RC2 compatible markup.
.container
.row
.col-12.col-sm-12.col-lg-6.col-lg-offset-3
%h1= t('titles.sign_in')
# Please do notice a custom form builder is in use here!
= custom_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
= f.error_notification
.form-inputs
= f.input :email, required: false, autofocus: true
= f.input :password, required: false
= f.input :remember_me, inline_label: t('activerecord.attributes.user.remember_me'), as: :boolean, label_html: { class: "visuallyhidden" } if devise_mapping.rememberable?
# I also added .form-group here, but this is not critical and YMMV
.form-actions.form-group
= f.button :submit, t('actions.log_in'), class: "btn btn-default"
# app/helpers/simple_form_extensions_helper.rb
# See https://github.com/plataformatec/simple_form#custom-form-builder
module SimpleFormExtensionsHelper
def custom_form_for(object, *args, &block)
options = args.extract_options!
simple_form_for(object, *(args << options.merge(builder: CustomFormBuilder)), &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment