7

Ho un modulo creato utilizzando la gemma simple_form che popola 2 modelli utilizzando attributi nidificati. Voglio verificare se ci sono errori e visualizzare un nuovo blocco. Tuttavia, non sono sicuro di come accedere correttamente al messaggio di errore per l'attributo location del modello Booking.Accesso ai messaggi di errore per il campo attributo nidificato

class Booking < ActiveRecord::Base 
    belongs_to :customer 

    attr_accessible :date_wanted, :location 
end 

e

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

    attr_accessible :name, :phone, :bookings_attributes 

    validates_presence_of :name, :phone 
end 

visualizzazione maschera:

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 

risposta

7

b è un'istanza di costruttore di forma, tenendo booking, in modo da poter provare:

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

Grazie! Sono in grado di vedere se ci sono messaggi di errore usando if 'b.object.errors [: location] .empty?'. – dspencer

Problemi correlati