Tôi muốn thay đổi hành vi mặc định của nút gửi trong simple_form để tôi không cần chỉ định rõ ràng: disable_with => true cho tất cả biểu mẫu của tôi. Làm thế nào tôi có thể thực hiện thay đổi cụ thể này trong simple_form.rb?Mặc định disable_with cho simple_form gửi
6
A
Trả lời
3
Thêm ghi đè này vào tệp simple_form.rb của tôi hoạt động như một sự quyến rũ!
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
5
Đây là một chút khác biệt trong phiên bản mới hơn của Rails, vì cài đặt thuộc tính disable_with không còn được dùng nữa. Tôi đã viết một bài báo về vấn đề này: http://www.railsonmaui.com/blog/2014/02/23/simple-form-and-disable-processing-by-default/
Dưới đây là đoạn code mới:
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
Và nhờ @Appster cho ý tưởng!
2
Theo ActionView::Helpers::FormBuilder.submit, f.button
tham gia 1 ~ 2 tham số, do đó, cả hai mã sau đều phải được làm việc.
f.submit "MyText", class: "my-btn"
f.submit class: "my-btn"
Trong trường hợp của tôi, thêm mã này để khởi tạo tập tin làm việc tốt.
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
Hy vọng điều đó sẽ hữu ích.
0
Nó không ghi đè lên bất kỳ hiện đĩa dữ liệu thuộc tính trên nút gửi mà là tương thích với 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)
Đối với tôi, điều này làm việc với một số nút và không phải người khác. Cụ thể, nó hoạt động với '<% = f.button: submit%>' nhưng không, ví dụ, '<% = f.button: button%>' Bạn có thể giải thích mã này đang làm gì để tôi có thể tìm ra cách Tôi có thể điều chỉnh cài đặt của riêng mình? – spume