2012-07-05 5 views
6

simple_form에서 제출 단추의 기본 동작을 변경하여 모든 양식에 disable_with => true를 명시 적으로 지정하지 않아도됩니다. simple_form.rb에서 어떻게이 특별한 변화를 만들 수 있습니까?simple_form 제출시 기본 disable_with

답변

3

내 override_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 
+0

와 호환 제출 버튼에있는 기존 데이터 - 속성을 무시하지 않았다, 이것은 일부 버튼이 아닌 다른 사람들과 함께 작동합니다. 특히, <% = f.button : submit %>'과 함께 작동하지만'<% = f.button : button %> '와 같이 작동하지 않습니다. 어떻게 코드가 수행되는지 설명해 주시겠습니까? 내 설정을 조정할 수 있습니까? – spume

5

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하기 위해 나는이에 대한 기사를 썼다!

2

ActionView::Helpers::FormBuilder.submit에 따르면, f.button은 1 ~ 2 매개 변수에 해당되므로 다음 두 코드가 모두 작동해야합니다. 파일을 초기화하기 위해이 코드를 추가 내 경우

  • f.submit class: "my-btn"
  • f.submit "MyText", 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 
      

      희망이 있습니다.

    0

    그것은 나를 위해 레일 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) 
    
    관련 문제