Module: Bootstrap::FormHelper

Defined in:
app/helpers/bootstrap/form_helper.rb

Overview

Rails helper methods associated with forms for Bootstrap.

Examples:

Bootstrap form-actions <div>

<%= form_actions do %>
  <%= submit_button_tag %>
  <%= cancel_button_tag %>
<% end %>

Constant Summary collapse

ArgumentError =
Class.new(StandardError)
InvalidButtonModifierError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#cancel_button_tag(text, type, size, options = {}) ⇒ String

Convenience method for standard “Cancel” button.

The text, type, and size arguments are all optional.

Has same semantics as calls to ButtonHelper#button except:

  • text defaults to “Cancel”

  • :url option is required

Examples:

cancel_button_tag(url: '/')
cancel_button_tag('Go Back', url: '/')
cancel_button_tag(:info, url: '/')
cancel_button_tag(:large, url: '/')
cancel_button_tag('Return', :small, :warning, url: '/', id: 'my-id')

Parameters:

  • text (String)

    text of link

  • type (Symbol)

    type of button

  • size (Symbol)

    size of button

  • options (Hash) (defaults to: {})

    All keys except :url become html attributes of the <a> tag

Options Hash (options):

  • :url (String)

    required

Returns:

  • (String)

    <a> tag styled as Bootstrap button

Raises:



33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/bootstrap/form_helper.rb', line 33

def cancel_button_tag(*args)
  options = canonicalize_options(args.extract_options!)
  raise(ArgumentError, "must pass a :url option") unless options.has_key?(:url)
  options = ensure_class(options, 'btn')
  
  args.map!(&:to_s)
  args.unshift("Cancel") if args.all? { |e| ::Bootstrap::ButtonHelper::BUTTON_ALL.include?(e) }

  button(*args, options)
end

#form_actions(options = {}) ⇒ String

Convience method for Bootstrap form action DIV.

Parameters:

  • options (Hash) (defaults to: {})

    options become html attributes of returned <div>

Returns:

  • (String)

    <div> with yielded block



48
49
50
51
52
53
54
55
# File 'app/helpers/bootstrap/form_helper.rb', line 48

def form_actions(options={})
  options = canonicalize_options(options)
  options = ensure_class(options, 'form-actions')
  
  (:div, options) do
    yield
  end
end

#submit_button_tag(text, type, size, options = {}) ⇒ String

Returns <input> similar to #submit_tag() but: x

  • styled like a Bootstrap button, type :primary

  • has :disable_with set to “Processing …”

See ButtonHelper for documentation on button type and size

submit_button_tag('Save')
submit_button_tag('Delete', :danger)
submit_button_tag('Big', :large)
submit_button_tag('With Options', :small, :info, id: 'my-id')

Parameters:

  • text (String)

    value of <input>

  • type (String, Symbol)

    type of button

  • size (String, Symbol)

    size of button

  • options (Hash) (defaults to: {})

    all options except :disable_with become html attributes for <input> tag

Options Hash (options):

  • :disable_with (String, false)

    either override or turn off the disabling of the button

Returns:

  • (String)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/bootstrap/form_helper.rb', line 72

def submit_button_tag(*args)
  options = canonicalize_options(args.extract_options!)
  
  value = if Bootstrap::ButtonHelper::BUTTON_ALL.include?(args.first.to_s)
    "Save changes"
  else
    args.shift.presence || "Save changes"
  end

  button_classes = if args.present?
    args.each { |e| raise(InvalidButtonModifierError, e.inspect) unless
       Bootstrap::ButtonHelper::BUTTON_ALL.include?(e.to_s) }
    ['btn'] + args.map { |e| "btn-#{e}" }
  else
    ['btn', 'btn-primary']
  end
  options = ensure_class(options, button_classes)

  disable_or_remove = options[:data] && options[:data].delete(:disable_with)

  unless disable_or_remove === false
    options[:data] ||= {}
    options[:data][:disable_with] = disable_or_remove || "Processing ..."
  end

  submit_tag(value, options)
end