Class: Bootstrap2FormBuilder::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/bootstrap2_form_builder/form_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_tagged_field(method_name) ⇒ Object

Generates form fields that work with Twitter Bootstrap. Automatically marks required things as required and places errors on matching inputs.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bootstrap2_form_builder/form_builder.rb', line 15

def self.create_tagged_field(method_name)
  define_method(method_name) do |label, *args|
    options = args.extract_options!

    custom_label = options[:label] || label.to_s.humanize
    label_class = options[:label_class] || "control-label"

    if @object.class.validators_on(label).collect(&:class).include? ActiveModel::Validations::PresenceValidator
      if label_class.nil?
        label_class = "required"
      else
        label_class = label_class + " required"
      end
    end

    control_group_class = "control-group"
    if @object.errors.messages[label]
      control_group_class = control_group_class + " error"
    end

  @template.("div",
    @template.("label",
              custom_label,
              :for => "#{@object_name}_#{label}",
              :class => label_class)  +
      @template.("div",
                super(label, *(args << options)) +
                    (@template.("p", options[:help_block], :class => "help-block") if options[:help_block]) +
                    (@template.("span", options[:help_inline], :class => "help-inline") if options[:help_inline]),
                :class => "controls"),
                :class => control_group_class)
  end
end

Instance Method Details

#submit(label, *args) ⇒ Object

Replace form submit input with styled buttons



4
5
6
7
8
9
# File 'lib/bootstrap2_form_builder/form_builder.rb', line 4

def submit(label, *args)
  options = args.extract_options!
  new_class = options[:class] || "btn btn-primary"
  @template.("div",
    super(label, *(args << options.merge(:class => new_class))), :class => "form-actions")
end