2010-07-24 11 views
10

रेल अनुप्रयोग पर रूबी में मैं उन क्षेत्रों से जानकारी का उपयोग करने की कोशिश कर रहा हूं जो सत्यापन में मॉडल से जुड़े नहीं हैं।रेल: सत्यापन फ़ील्ड का उपयोग करना जो सत्यापन में मॉडल के साथ असंबद्ध हैं

यहाँ एक उदाहरण के रूप में मॉडल के भाग (पूरे मॉडल थोड़े बड़े हो गया है) है:

class Scorecard < ActiveRecord::Base 
    belongs_to :course 
    belongs_to :user 

    validate :attributes_consistency 

    def attributes_consistency 
    # Executed for all scorecards. Checks if the user completed the hole attributes correctly 
    if (params[:no_fairways] and any_fairways? and !only_nine?) or (params[:no_fairways] and !any_h1_to_h9_score_blank and any_h1_to_h9_fairway? and only_nine?) or (params[:no_fairways] and !any_h10_to_h18_score_blank and any_h10_to_h18_fairway? and only_nine?) 
     errors.add_to_base("You inidicated that you missed all the fairways, but you also marked one or more fairways in the scorecard. Either uncheck the fairways mistakenly marked or uncheck the 'No fairways' checkbox.") 
    end 
    if (params[:no_girs] and any_girs? and !only_nine?) or (params[:no_girs] and !any_h1_to_h9_score_blank and any_h1_to_h9_gir? and only_nine?) or (params[:no_girs] and !any_h10_to_h18_score_blank and any_h10_to_h18_gir? and only_nine?) 
     errors.add_to_base("You inidicated that you missed all the greens, but you also marked one or more greens in the scorecard. Either uncheck the marked greens on the scorecard or uncheck the 'No GIRs' checkbox.") 
    end 
    end # attributes_consistency 


    def any_h1_to_h9_score_blank? 
    h1_score.blank? or h2_score.blank? or h3_score.blank? or h4_score.blank? or h5_score.blank? or h6_score.blank? or h7_score.blank? or h8_score.blank? or h9_score.blank? 
    end 
    def any_h10_to_h18_score_blank? 
    h10_score.blank? or h11_score.blank? or h12_score.blank? or h13_score.blank? or h14_score.blank? or h15_score.blank? or h16_score.blank? or h17_score.blank? or h18_score.blank? 
    end 

    def any_h1_to_h9_fairway? 
    h1_fairway? or h2_fairway? or h3_fairway? or h4_fairway? or h5_fairway? or h6_fairway? or h7_fairway? or h8_fairway? or h9_fairway? 
    end 
    def any_h10_to_h18_fairway? 
    h10_fairway? or h11_fairway? or h12_fairway? or h13_fairway? or h14_fairway? or h15_fairway? or h16_fairway? or h17_fairway? or h18_fairway? 
    end 

    def any_h1_to_h9_gir? 
    h1_gir? or h2_gir? or h3_gir? or h4_gir? or h5_gir? or h6_gir? or h7_gir? or h8_gir? or h9_gir? 
    end 
    def any_h10_to_h18_gir? 
    h10_gir? or h11_gir? or h12_gir? or h13_gir? or h14_gir? or h15_gir? or h16_gir? or h17_gir? or h18_gir? 
    end 

तो मैं कैसे मॉडल से params उपयोग कर सकते हैं?

उत्तर

15

पैराम को मॉडल पर छेड़छाड़ न करने दें। उस मामले में नियंत्रक होने का कोई मतलब नहीं है। इसके बजाय, Railscasts से इस एपिसोड को चेकआउट करें जो वर्चुअल विशेषताओं के बारे में बात करता है जो डेटाबेस में नहीं जाते हैं लेकिन अभी भी सत्यापन के लिए उपयोग किए जा सकते हैं।

आपको वर्चुअल विशेषताओं के लिए संबंधित मॉडल विशेषता की आवश्यकता नहीं है। राज्य को पकड़ने वाले @no_fairways जैसे कक्षा में स्थानीय गुणों को परिभाषित करें।

class ScoreCard < ActiveRecord::Base 
    # define attributes and accessors for both fields 
    attr_accessor :no_fairways, :no_girs 

    .. 
end 

अब अंदर आप के रूप में, तुम सिर्फ लिख सकते हैं:

<% form_for @scorecard %> 
    <%= f.check_box :no_fairways %> 
<% end %> 
+0

यह एक अच्छा समाधान होगा, लेकिन क्या आप इसका उदाहरण दे सकते हैं कि मैं इसे कैसे कार्यान्वित कर सकता हूं? उस विशेष रेलस्कास्ट में "वर्चुअल विशेषताएँ" वास्तव में वे विधियां हैं जो वास्तविक विशेषताओं में हेरफेर करती हैं। मेरे पास कोई विशेषता नहीं है कि 'no_fairways' या' no_girs' से जुड़ा जा सकता है। – Jay

+0

@ जेम्स - वर्चुअल विशेषता को किसी भी डेटाबेस विशेषता से संबद्ध नहीं होना चाहिए। आप इंस्टेंस विशेषताओं को परिभाषित कर सकते हैं जो ठीक से काम करेंगे। अद्यतन उत्तर – Anurag

+0

ध्यान दें कि चेकबॉक्स से जुड़े attr में trythy या falsy value नहीं होगा। डिफ़ॉल्ट रूप से 'check_box' मार्कअप उत्पन्न करता है जिसके परिणामस्वरूप अनचेक किए जाने पर' 0 '' स्ट्रिंग और '1 "' चेक किया जाता है। यहां तक ​​कि '0" .present? 'सच है। – kolen

3

मिले समाधान, शब्दावली के लिए धन्यवाद हालांकि, "आभासी विशेषता" गूगल searchin साथ मदद की।

इसे पूरा करने का सबसे साफ तरीका उन गुणों को बनाना है जो डेटाबेस का हिस्सा नहीं हैं लेकिन फिर भी मॉडल का हिस्सा हैं। मेरे मामले में मैंने इसे मॉडल में रखा:

attr_accessor :no_fairways 
attr_accessor :no_girs 

यह आसान है! अब @scorecard.no_fairways और @scorecard.no_girs किसी अन्य विशेषता की तरह कार्य करते हैं लेकिन डेटाबेस का हिस्सा नहीं हैं।

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