Skip to content

Instantly share code, notes, and snippets.

@shamil614
Forked from cbmeeks/bootstrap_form_builder.rb
Created February 20, 2012 22:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shamil614/1871860 to your computer and use it in GitHub Desktop.
Save shamil614/1871860 to your computer and use it in GitHub Desktop.
Twitter Bootstrap 2.0 Form Builder & Devise
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :capture, :content_tag, :tag, to: :@template
%w[text_field text_area password_field collection_select email_field].each do |method_name|
define_method(method_name) do |name, *args|
errors = object.errors[name].any?? " error" : ""
error_msg = object.errors[name].any?? content_tag(:span, object.errors[name].join(","), class: "help-inline") : ""
content_tag :div, class: "control-group#{errors}" do
field_label(name, *args) + content_tag(:div, class: "controls") do
super(name, *args) + " " + error_msg
end
end
end
end
def check_box(name, *args)
content_tag :div, class: "clearfix" do
content_tag(:div, class:"input") do
content_tag(:ul, class:"inputs-list") do
content_tag(:li) do
content_tag(:label) do
super(name, *args) + content_tag(:span) do
field_label(name, *args)
end
end
end
end
end
end
end
def div(*args, &block)
options = args.extract_options!
data = block_given? ? capture(&block) : ''
content_tag(:div, data, class: options[:class])
end
def submit(*args)
super(*args, class: "btn-primary")
end
private
def field_label(name, *args)
options = args.extract_options!
required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator}
label(name, options[:label], class: "control-label" + (if required then "required" else "" end))
end
def objectify_options(options)
super.except(:label)
end
end
= form_for(resource, :builder => BootstrapFormBuilder, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-horizontal'}) do |f|
=# devise_error_messages!
= f.text_field :first_name
= f.text_field :last_name
= f.email_field :email
= f.password_field :password
= f.password_field :password_confirmation
.form-actions
= f.submit "Sign up"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment