Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active February 27, 2023 16:33
Show Gist options
  • Save maxivak/57a2fb71afeb9efcf771 to your computer and use it in GitHub Desktop.
Save maxivak/57a2fb71afeb9efcf771 to your computer and use it in GitHub Desktop.
Rails simple_form custom submit button. simple_form, bootstrap 3

Custom submit button in simple_form and bootstrap 3

Objective

It is easy to add wrappers for input components in simple_form, but it takes some time to modify rendering of buttons ("f.button").

We want to have this in our form:

= f.button :submit_cancel, 'Save'

where "submit_cancel" is the name of our new component,

that will render submit button as:

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

or with :cancel option

=simple_form_for @order do |f|
  ... fields here ...

  = f.button :submit_cancel, 'Save', :cancel=>orders_path

will render

<div class="form-group">
  <div class="col-sm-offset-4 col-sm-10">
    <input class="btn btn-primary" type="submit" value="Save" name="commit">
    <a href="...">Cancel</a>
  </div>
</div>

Step 0.

install simple_form with wrappers for bootstrap:

rails generate simple_form:install --bootstrap

that will create two files:

config/initializers/simple_form.rb
config/initializers/simple_form_bootstrap.rb

Step 1. add custom component to simple_form

create a new file lib/simple_form_extensions/button_components.rb

module ButtonComponents
    def submit_cancel(*args, &block)
      template.content_tag :div, :class => "form-group" do
      template.content_tag :div, :class => "col-sm-offset-4 col-sm-10" do
        options = args.extract_options!
  
        # class
        options[:class] = [options[:class], 'btn-primary'].compact
  
        #
        args << options


        # with cancel link
        if cancel = options.delete(:cancel)
          submit(*args, &block) + '&nbsp;&nbsp;'.html_safe + template.link_to(I18n.t('simple_form.buttons.cancel'), cancel)
        else
          submit(*args, &block)
        end
  
      end
      end
    end
  
end

SimpleForm::FormBuilder.send :include, ButtonComponents

This component is suitable for horizontal form in bootstrap 3. To use it in other forms (vertical, inline) modify tags "template.content_tag" appropriately.

Step 2. Include component in initializer

# config/initializers/simple_form_bootstrap.rb

require 'button_components.rb'

SimpleForm.setup do |config|
    ...
end

Step 3. Translation

/config/locales/simple_form.en.yml

en: simple_form: buttons: cancel: "Cancel"

<div class="form-group">
<div class="col-sm-offset-4 col-sm-10">
<input class="btn btn-primary" type="submit" name="commit" value="Save" />
</div>
</div>
@billguy
Copy link

billguy commented Feb 28, 2016

Neat. I had to do:

require 'simple_form_extensions/button_components.rb'

@aldrienht
Copy link

aldrienht commented Jul 15, 2016

@billguy, you're right, otherwise we'll get cannot load such file -- button_components.rb

@nononoy
Copy link

nononoy commented Nov 18, 2016

you're awesome. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment