Skip to content

Instantly share code, notes, and snippets.

@eraserewind
Created July 28, 2013 19:08
Show Gist options
  • Save eraserewind/6099719 to your computer and use it in GitHub Desktop.
Save eraserewind/6099719 to your computer and use it in GitHub Desktop.
bootstrap 3 horizontal form rails builder
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment