7
Tôi có biểu mẫu được tạo bằng cách sử dụng đá quý simple_form để điền 2 mô hình bằng cách sử dụng thuộc tính lồng nhau. Tôi muốn kiểm tra nếu có bất kỳ lỗi nào và hiển thị một khối mới. Tuy nhiên, tôi không chắc chắn cách truy cập chính xác thông báo lỗi cho thuộc tính location
của mô hình Booking
.Truy cập thông báo lỗi cho trường thuộc tính lồng nhau
class Booking < ActiveRecord::Base
belongs_to :customer
attr_accessible :date_wanted, :location
end
và
class Customer < ActiveRecord::Base
has_many :bookings
accepts_nested_attributes_for :bookings
attr_accessible :name, :phone, :bookings_attributes
validates_presence_of :name, :phone
end
xem Mẫu:
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
Cảm ơn! Tôi có thể xem nếu có bất kỳ thông báo lỗi nào bằng cách sử dụng nếu 'b.object.errors [: location] .empty?'. – dspencer