Skip to content

Instantly share code, notes, and snippets.

@eraserewind
Created July 28, 2013 19:08

Revisions

  1. eraserewind created this gist Jul 28, 2013.
    36 changes: 36 additions & 0 deletions bootstrap_horizontal_form.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    class BootstrapHorizontalForm < ActionView::Helpers::FormBuilder

    def text_field(method, options={})
    width = options[:width] || 10
    t = @template
    t.content_tag(:div, :class => "form-group#{' has-error' unless @object.errors[method].blank?}") {
    t.concat(t.label(@object_name, method, class: 'col-lg-2 control-label'))
    t.concat(t.content_tag(:div, class: "col-lg-#{width}") {
    t.concat(t.text_field(@object_name, method, options.merge(class: 'form-control')))
    if @object.errors[method].present?
    t.concat(t.content_tag(:span, @object.errors[method].join(', '), :class => 'help-block'))
    end
    })
    }
    end

    def check_box(method, options={})
    t = @template
    t.content_tag(:div, class: "form-group #{' has-error' unless @object.errors[method].blank?}") {
    t.concat(t.content_tag(:div, class: 'col-lg-10 col-offset-2') {
    t.concat(t.content_tag(:div, class: 'checkbox') {
    t.concat(t.check_box(@object_name, method))
    t.concat(t.label(@object_name, method))
    })
    })
    }
    end

    def submit(value=nil, options={})
    @template.content_tag(:div, class: 'col-lg-10 col-offset-2') {
    super(value, options.merge(class: 'btn btn-primary'))
    }
    end

    end