7

मेरे पास सरल_फॉर्म मणि का उपयोग करके एक फ़ॉर्म बनाया गया है जो नेस्टेड विशेषताओं का उपयोग करके 2 मॉडल पॉप्युलेट करता है। मैं जांचना चाहता हूं कि क्या कोई त्रुटि है और एक नया ब्लॉक प्रदर्शित करें। हालांकि, मुझे यकीन नहीं है कि Booking मॉडल के location विशेषता के लिए त्रुटि संदेश को सही तरीके से कैसे पहुंचाया जाए।नेस्टेड विशेषता फ़ील्ड के लिए त्रुटि संदेशों तक पहुंच

class Booking < ActiveRecord::Base 
    belongs_to :customer 

    attr_accessible :date_wanted, :location 
end 

और

class Customer < ActiveRecord::Base 
    has_many :bookings 
    accepts_nested_attributes_for :bookings 

    attr_accessible :name, :phone, :bookings_attributes 

    validates_presence_of :name, :phone 
end 

प्रपत्र दृश्य:

simple_form_for @customer, {:html => { :class => "form-horizontal" }} do |f| 
    = f.input :name 
    = f.input :phone 
    = f.simple_fields_for :bookings do |b| 
    = b.input :date 
    = b.input :location 
    - if @customer.errors[:appointments_attributes][:location] 
     # insert code if any validation errors for the date field were found 
    = f.button :submit 

उत्तर

7

b, booking पकड़े, प्रपत्र बिल्डर का एक उदाहरण है, ताकि आप की कोशिश कर सकते हैं:

# ... 
if b.object.errors[:location] 
# ... 
+1

धन्यवाद! मैं यह देखने में सक्षम हूं कि 'b.object.errors [: location] .empty? 'का उपयोग कर कोई त्रुटि संदेश है या नहीं। – dspencer

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