6

I want to change the default behavior of my submit button in simple_form so that I needn't explicitly specify :disable_with => true for all my forms. How can I make this particular change in the simple_form.rb?

5 Answers 5

6

This is a little different in newer versions of Rails, as setting the property disable_with is deprecated. I wrote an article on this: http://www.railsonmaui.com/blog/2014/02/23/simple-form-and-disable-processing-by-default/

Here's the new code:

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(field, options = {})
    data_disable_with = { disable_with: 'Processing...' }
    options[:data] = data_disable_with.merge(options[:data] || {})
    submit_without_override(field, options)
  end
  alias_method_chain :submit, :override
end

And thanks to @Appster for the idea!

3

Adding this override to my simple_form.rb worked like a charm!

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(field, options = {})
    submit_without_override(field, {:disable_with => 'saving...'}.merge(options))
  end
  alias_method_chain :submit, :override
end
1
  • For me, this works with some buttons and not others. Specifically, it works with <%= f.button :submit %> but not, for example, <%= f.button :button %> Could you explain what this is code is doing so I can work out how I might tweak my own settings?
    – spume
    Jan 20, 2014 at 14:59
2

According to ActionView::Helpers::FormBuilder.submit, f.button accespts 1~2 parameters, so both of following codes should be worked.

  • f.submit "MyText", class: "my-btn"
  • f.submit class: "my-btn"

In my case, adding this codes to initialize file worked fine.

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(value=nil, options={})
    value, options = nil, value if value.is_a?(Hash)
    data_disable_with = { disable_with: 'Processing...' }
    options[:data] = data_disable_with.merge(options[:data] || {})
    submit_without_override(value, options)
  end
  alias_method_chain :submit, :override
end

Hope it helps.

1

It didn't override any existing data- attributes on the submit button which is compatible with Rails 5.

module DisableDoubleClickOnSimpleForms
  def submit(field, options = {})
    if field.is_a?(Hash)
      field[:data] ||= {}
      field[:data][:disable_with] ||= field[:value] || 'Processing...'
    else
      options[:data] ||= {}
      options[:data][:disable_with] ||= options[:value] || 'Processing...'
    end
    super(field, options)
  end
end

SimpleForm::FormBuilder.prepend(DisableDoubleClickOnSimpleForms)
0

If you are trying to change it for a single item and not everything I thought I would share this.

I am using Rails v6.1.5 and Simple form and I ran into this Simple Form issue today.

I was trying to change the value of the submit button once pressed from

'Change LOGO'

to

'Updating LOGO'

I tried the normal way:

= f.button :submit, class: 'btn btn-sm btn-primary', value: 'Change LOGO', id: 'submitLogo',
                  onclick: "logoSubmitButton(event)", disable_with: 'Updating LOGO'

It gave no error but it kept changing the button to 'Updating Company' which is the name of the instance variable and the Model @company and Company respectively.

This is what I had to use to make it work. I didn't have to change anything on the simple_form.rb initializer file.

= f.button :submit, class: 'btn btn-sm btn-primary', value: 'Change LOGO', id: 'submitLogo',
                  onclick: "logoSubmitButton(event)", data: {disable_with: 'Updating LOGO'}

This is the golden nugget that made it work:

data: {disable_with: 'Updating LOGO'}

I hope this helps someone else...

Scott

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.