Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: custom wrapper for submit button in simple_form?

I am using simple_form gem with bootstrap 3. I want to have a wrapper for submit button

Now it shows HTML as

<form id="new_order" ...>
    ...

   <input class="btn btn-primary" type="submit" value="Save" name="commit">
</form>

I want to write a wrapper so the HTML would be:

<form id="new_order" ...>
  ...

  <div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
        <input class="btn btn-primary" type="submit" value="Save" name="commit">
     </div>
 </div>

I got this so far:

app/initializers/simple_form_bootstrap.rb:

options[:wrapper] = :horizontal_form
options[:wrapper_mappings] = {
  check_boxes: :horizontal_radio_and_checkboxes,
  radio_buttons: :horizontal_radio_and_checkboxes,
  file: :horizontal_file_input,
  boolean: :horizontal_boolean,

 # what to write here??
 # submit: :horizontal_submit_button

}

and this is my wrapper:

  config.wrappers :horizontal_submit_button, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
     b.use :html5
     b.use :placeholder

     b.wrapper tag: 'div', class: 'col-sm-offset-2 col-sm-10' do |ba|
       ba.use :input
       # some coe goes here, maybe
     end
  end

Which input type to use in wrapper_mappings for submit button? How to write that wrapper?

like image 523
Max Ivak Avatar asked Dec 17 '14 21:12

Max Ivak


2 Answers

I was looking to do the same thing today. I couldn't find a way to do it through the wrapper API, oddly, but by default Simple Form likes to wrap its inputs in a div.form-inputs and its submit button in a div.form-actions. So I styled the div.form-actions to @extend .col-md-offset-4 to get the result I wanted.

Hope this helps!

like image 135
sevenseacat Avatar answered Sep 23 '22 20:09

sevenseacat


Simple form button is basic wrapper around rails submit helper. All it does is it adds button_class defined in simple form initializer and send it to submit, here is what it look like right now:

https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L211

def button(type, *args, &block)
  options = args.extract_options!.dup
  options[:class] = [SimpleForm.button_class, options[:class]].compact
  args << options
  if respond_to?(:"#{type}_button")
    send(:"#{type}_button", *args, &block)
  else
    send(type, *args, &block)
  end
end

There is no logic for using wrapper api here. But that if statement lets you define your own button method. If you use f.button :submit in your forms than put this in an initializer (or decorator so you don't have to restart the server while customizing):

module SimpleForm
  class FormBuilder
    def submit_button(*args, &block)
      ActionController::Base.helpers.content_tag(:div, class: 'form-actions') do
        submit(*args, &block)
      end
    end
  end
end

You don't have to use content_tag helpers, any html_safe string will work.

like image 39
Alex Avatar answered Sep 23 '22 20:09

Alex