Module: RailsBootstrapWidgets::ModalHelper

Defined in:
app/helpers/rails-bootstrap-widgets/modal_helper.rb

Instance Method Summary collapse

Instance Method Details

To create modal form in a view, put your fields into modal_form_widget(options = {}, &block).

modal_form_widget (
  title: 'modal window title',              # required
  action: 'path to corresponding route',    # required
  method: 'request method for the form',    # "post" by default, "post", "get", "put" or "delete" extected
  button: 'text on the submit button',      # I18n.t("buttons.submit") by default
  cancel: 'text on the cancel form button'  # I18n.t("buttons.cancel") by default
) { "" }                                    # html code of form fields

The widget puts a list of fields (inputs, selectors etc.) into a modal form window (presented by <form id='modal' class='modal fade'...>) For example, the call

modal_form_widget(title: 'Title', button: 'Button', method: :put, action: '/some_addr', cancel: "Cancel") { "content" }

Will provide html:

<form accept-charset="UTF-8" action="/some_addr" class="modal fade" data-remote="true" id="modal" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="&#x2713;" />
  </div>
  <input id="_method" name="_method" type="hidden" value="put" />
  <header class="modal-header">
    <button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
    <h1>Title</h1>
  </header>
  <div class="modal-body">content</div>
  <div class="modal-footer">
    <a href="#" aria-hidden="true" class="btn" data-dismiss="modal">Cancel</a>
    <input class="btn btn-primary" id="submit" name="commit" type="submit" value="Button" />
  </div>
</form>


37
38
39
40
41
42
43
44
45
# File 'app/helpers/rails-bootstrap-widgets/modal_helper.rb', line 37

def modal_form_widget(options = {}, &block)
  if (options = _prepare(options, :form)) && (content = block_given? ? capture(&block) : nil)
    form_tag(options[:action], method: :post, id: "modal", class: "modal fade", remote: true) do
      (_hidden(options[:method]) << _header(options[:title]) << _body(content) << _footer(:submit, options[:button], options[:cancel])).html_safe
    end
  else
    ''
  end
end

To create modal window in a view, put its content into modal_view_widget(options = {}, &block).

modal_view_widget (
  title: 'modal window title',              # required
  href: 'path for redirection to details',  # required
  redirect: true|false,                     # whether redirect button should be shown in the modal window footer (true by default),
  button: 'text on the redirect button',    # I18n.t("buttons.details") by default
  cancel: 'text on the cancel form button'  # I18n.t("buttons.cancel") by default
) { "" }                                    # html code of form fields

The widget puts html code into a modal window (presented by <div id='modal' class='modal fade'>) For example, the call

modal_view_widget(title: 'Title', button: 'Button', href: '/some_addr', cancel: "Cancel") { "content" }

Will provide html:

<div class="modal fade" id="modal">
  <header class="modal-header">
    <button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
    <h1>Title</h1>
  </header>
  <div class="modal-body">content</div>
  <div class="modal-footer">
    <a href="#" aria-hidden="true" class="btn" data-dismiss="modal">Cancel</a>
    <a href="/some_addr" class="btn btn-primary">Button</a>
  </div>
</div>


75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/rails-bootstrap-widgets/modal_helper.rb', line 75

def modal_view_widget(options = {}, &block)
  content = block_given? ? capture(&block) : nil
  if (options = _prepare(options, :view)) && content
    (:div, id: "modal", class: "modal fade") do
      (_header(options[:title]) << _body(content) << _footer(:button, options[:button], options[:cancel], options[:href], options[:redirect])).html_safe
    end
  else
    ''
  end
end