2012-09-03 8 views
14

मैं twitter bootstrap के साथ प्रमाणीकरण के लिए sorcery का उपयोग कर रहा हूं।डिफ़ॉल्ट रेल त्रुटि त्रुटि को कैसे बदलें "field_with_errors"

मैं डिफ़ॉल्ट रेल <div class="field_with_errors"> को बदलकर ट्विटर के बूटस्ट्रैप की शैली में अपने साइनअप फॉर्म पर अपने त्रुटि संदेशों को स्टाइल करना चाहता हूं जो DOM में जोड़ा जाता है।

ऐसा कुछ करने के लिए रेल सम्मेलन क्या है?

मुझे लगता है कि आप कुछ जावास्क्रिप्ट जोड़ सकते हैं जो <div class="field_with_errors"> का नाम बदलने के लिए डीओएम में हेरफेर करता है, लेकिन यह एक हैक जैसा लगता है। ऐसा लगता है कि रेल में इसे ओवरराइड करने का एक तरीका होना चाहिए, लेकिन मैं यह नहीं समझ सकता कि यह कहां करना है।

यह कैसे बूटस्ट्रैप अपने त्रुटि अपने प्रपत्र त्रुटि शैलियों में बनाया का उपयोग करने को चिह्नित करने की आवश्यकता है है:

<div class="control-group error"> 
    <label class="control-label" for="inputError">Input with error</label> 
    <div class="controls"> 
    <input type="text" id="inputError"> 
    <span class="help-inline">Please correct the error</span> 
    </div> 
</div> 
+0

मुझे लगता है कि आप के लिए यह
http लिए देख रहे हैं : //stackoverflow.com/questions/5267998/rails-3-field-with-errors-wrapper-changes-the-page-appearance-how-to-avoid-t – Prem

+0

@prem: धन्यवाद! खुद से लिंक करने वाला था। –

उत्तर

24

ऊपर के लिंक से, अगर आप अंदर class Application < Rails::Application के निम्नलिखित डाल config/application.rb

config.action_view.field_error_proc = Proc.new { |html_tag, instance| 
    "<div class=\"field_with_errors control-group error\">#{html_tag}</div>".html_safe 
} 

जब भी सत्यापन विफल रहता है तो आपके इनपुट टैग में उनके चारों ओर एक लाल मार्कर होगा

+4

इनपुट को एक लाइन कूदने के लिए लेबल के लिए क्षैतिज में यह कारण होता है, क्या वहां कोई काम है? – Nayish

+0

मैंने 'एक्शन व्यू :: बेस.फील्ड_error_proc = Proc.new {| html_tag, instance | द्वारा प्रेरित कुछ जोड़कर एक ही क्रिया के लिए व्यवहार को बदल दिया। "

#{html_tag}
" .html_safe} 'किसी विशेष क्रिया विधि के अंदर। [रेफरी] (http://stackoverflow.com/a/11157784/664833) – user664833

1

बूटस्ट्रैप 3.2 के लिए आप sth का उपयोग कर सकते हैं। config/initializers/field_error_proc.rb फ़ाइल (नहीं मौजूद है, तो एक बनाने के)

यह थोड़ा से कोड संशोधित किया गया है अंदर

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| 
    html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe 
    # add nokogiri gem to Gemfile 

    form_fields = [ 
    'textarea', 
    'input', 
    'select' 
    ] 

    elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ') 

    elements.each do |e| 
    if e.node_name.eql? 'label' 
     html = %(<div class="control-group has-error">#{e}</div>).html_safe 
    elsif form_fields.include? e.node_name 
     if instance.error_message.kind_of?(Array) 
     html = %(<div class="control-group has-error">#{html_tag}<span class="help-block">&nbsp;#{instance.error_message.uniq.join(', ')}</span></div>).html_safe 
     else 
     html = %(<div class="control-group has-error">#{html_tag}<span class="help-block">&nbsp;#{instance.error_message}</span></div>).html_safe 
     end 
    end 
    end 
    html 
end 

प्लेस इस कोड: इस तरह (अपने Gemfile को nokogiri मणि जोड़ने) Overriding ActionView::Base.field_error_proc in Rails

0

ध्यान दें कि यदि आप SimpleForm का उपयोग कर रहे हैं, application.rb में एक प्रक्रिया का उपयोग करने का स्वीकार्य उत्तर काम नहीं करेगा। इसके बजाय, आपको simple_form प्रारंभकर्ता को संपादित करना चाहिए। बूटस्ट्रैप 3.2 का उपयोग कर उदाहरण के लिए: ऑन रेल्स 5

config.wrappers :default, class: :input, 
    hint_class: :field_with_hint, error_class: :'has-error' do |b| 
    [...] 
    b.use :hint, wrap_with: { tag: :span, class: :'text-warning' } 
    b.use :error, wrap_with: { tag: :span, class: :'text-danger' } 
end 
1

ट्विटर बूटस्ट्रैप 3.3.6, यह एक प्रारंभकर्ता customize_error.rb में चला जाता है और मेरे लिए काम करता है:

ActionView::Base.field_error_proc = proc { |html_tag, _instance| "<div class=\"has-error\">#{html_tag}</div>".html_safe } 
संबंधित मुद्दे