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?

4

4 回答 4

5

这在较新版本的 Rails 中略有不同,因为不推荐设置属性 disable_with。我为此写了一篇文章:http ://www.railsonmaui.com/blog/2014/02/23/simple-form-and-disable-processing-by-default/

这是新代码:

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

感谢@Appster 的想法!

于 2014-02-24T08:58:45.883 回答
3

将此覆盖添加到我的 simple_form.rb 就像一个魅力!

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
于 2012-07-23T10:31:45.100 回答
2

根据ActionView::Helpers::FormBuilder.submitf.button接受 1~2 个参数,因此以下两个代码都应该工作。

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

就我而言,添加此代码来初始化文件工作正常。

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

希望能帮助到你。

于 2014-09-03T10:43:36.990 回答
1

它没有覆盖与 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)
于 2016-04-25T06:27:17.770 回答