2013-07-08 9 views
5

मेरे पास सभी पृष्ठों में कैलेंडर का उपयोग करने के लिए वैश्विक रूप से एक Jquery UI Datepicker फ़ंक्शन है। मैं तो बस मेरे विचार में मेरे datepicker क्षेत्र पोस्ट,जावास्क्रिप्ट फ़ंक्शन jquery-AJAX रेल सत्यापन सत्यापन त्रुटि में काम नहीं कर रहा है

<div class="field"> 
    <%= f.label :Renewal_Date %> 
    <%= f.text_field :Renewal_Date, readonly: 'readonly', data: {field_type: date}} 
</div> 

मैं एक अलग जावास्क्रिप्ट फाइल करने के लिए ऊपर फ़ंक्शन को कॉल

var showDatePickers = function() { 
    $('[data-field-type="date"]').datepicker({ 
    dateFormat: "yy-mm--dd", 
    showOn: "both", 
    buttonImageOnly: true, 
    buttonImage: "/assets/blue-calendar-icon.png", 
    buttonText: "Calendar" 
    }); 
} 

$(showDatePickers); 

: मैं निम्नलिखित की तरह एक अलग जावास्क्रिप्ट पृष्ठ बनाया।

$(function() { 
    if ($('html.asset_contracts').length == 1) { 
    $(document.body).on('ajax:success', '.new_asset_contract, .edit_asset_contract',  showDatePickers); 
    } 
}); 

पृष्ठ लोड होने, संपादित करने और नई कार्रवाई के समय यह ठीक काम कर रहा है। लेकिन जब रेल सत्यापन त्रुटि त्रुटि दिनांकक काम करता है तो काम नहीं कर रहा है। यह खाली text_field दिखा रहा है।

एफवाईआई: यह एक अजाक्स पृष्ठ है और new, create, update and edit कार्रवाई ajax पृष्ठों के रूप में काम कर रही है। तो, मैं अपने फार्म में remote: true जोड़ा गया है और मैं new.js, edit.js, create.js and update.js

यह मेरी नियंत्रक है है,

def create 
    @contract = Asset::Contract.new(params[:asset_contract]) 

    respond_to do |format| 
     if @contract.save 
     format.html { redirect_to asset_contracts_path, notice: "Successfully Created" } 
     format.js 
     format.json { render json: @contract, status: :created, location: @contract } 
     else 
     format.html { render action: "new" } 
     format.js 
     format.json { render json: @contract.errors, status: :unprocessable_entity } 
     end 
    end 
    end 



    def update 
    @contract = Asset::Contract.find(params[:id]) 

    respond_to do |format| 
     if @contract.update_attributes(params[:asset_contract]) 
     format.html { redirect_to asset_contracts_path, notice: "Succesfully Updated" } 
     format.js 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.js 
     format.json { render json: @contract.errors, status: :unprocessable_entity } 
     end    
    end 
    end 

धन्यवाद

+0

जब सत्यापित करता त्रुटि, हो js जाँच datetime क्षेत्र seletor '[डेटा मैदान प्रकार =" date "]' – yxf

उत्तर

5

तुम इतनी तरह datepickers बना रहे हैं:

$(document.body).on('ajax:success', '.new_asset_contract, .edit_asset_contract',  showDatePickers); 

हालांकि, यह केवल तभी चलाएगा जब AJAX कॉल सफल हो, तो आपको त्रुटियों के लिए एक हैंडलर की भी आवश्यकता होगी।

ऐसा प्रतीत होता है कि आप नामांकित घटनाओं का उपयोग कर रहे हैं और मुझे यह नहीं लगता कि jQuery दस्तावेज़ में संदर्भित है। आप शायद वैश्विक AJAX घटनाओं का उपयोग करना चाहेंगे (उदा।, AJAXComplete, AJAXError, आदि)।

त्रुटि मामले को संभालने के लिए आपको या तो ajaxError के लिए एक अलग हैंडलर संलग्न करने की आवश्यकता होगी याके स्थान पर केवल ajaxComplete ईवेंट का उपयोग करें। जब तक आपको विशिष्ट त्रुटि प्रबंधन की आवश्यकता न हो, ajaxComplete जाने का तरीका है, क्योंकि आपको केवल एक हैंडलर लिखना/बनाए रखना होगा। JQuery 1.8 के अनुसार, वैश्विक घटनाएं document पर ट्रिगर की गई हैं। आप किसी भी अन्य चयनकर्ताओं के बिना दस्तावेज़ में आपके श्रोता संलग्न करने की आवश्यकता होगी:

$(document).on('ajaxComplete', showDatePickers); 

आप Ajax Events पृष्ठ पर jQuery AJAX घटनाओं के बारे में और अधिक पढ़ सकते हैं।

+0

मैं 'ajax के साथ की कोशिश की संशोधित करता है, तो: पूरा, ajax: त्रुटि, ajax: कोई भाग्य success'। धन्यवाद। – Vinay

+0

मैंने नामांकित घटनाओं के बजाय वैश्विक घटनाओं का उपयोग करने के लिए अद्यतन किया। इसे आज़माएं और देखें कि यह काम करता है या नहीं। – cfs

+0

'AJAX: पूरा' कामकाजी साथी नहीं है। मुझे 'AJAXError' के लिए एक अलग हैंडलर संलग्न करने की कोशिश करने की आवश्यकता हो सकती है। कृपया मुझे ऐसा करने के लिए मार्गदर्शन करें। – Vinay

संबंधित मुद्दे