7

Ich habe ein Formular erstellt mit dem Simple_form-Juwel, das 2 Modelle mit verschachtelten Attributen füllt. Ich möchte prüfen, ob Fehler vorliegen und einen neuen Block anzeigen. Ich bin mir jedoch nicht sicher, wie ich richtig auf die Fehlermeldung für das location Attribut des Booking Modells zugreifen kann.Zugriff auf Fehlermeldungen für verschachteltes Attributfeld

class Booking < ActiveRecord::Base 
    belongs_to :customer 

    attr_accessible :date_wanted, :location 
end 

und

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

    attr_accessible :name, :phone, :bookings_attributes 

    validates_presence_of :name, :phone 
end 

Formularansicht:

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 

Antwort

7

b eine Instanz von Formular-Builder ist, booking halten, so können Sie versuchen:

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

Dank! Ich bin in der Lage zu sehen, ob es Fehlermeldungen gibt, wenn ich 'b.object.errors [: location] .empty? '. – dspencer

Verwandte Themen