gpt4 book ai didi

ruby-on-rails - 访问嵌套属性字段的错误消息

转载 作者:行者123 更新时间:2023-12-04 11:56:11 25 4
gpt4 key购买 nike

我有一个使用 simple_form gem 创建的表单,它使用嵌套属性填充 2 个模型。我想检查是否有任何错误并显示一个新块。但是,我不确定如何正确访问 location 的错误消息。 Booking 的属性模型。

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

表格 View :
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

最佳答案

b是表单生成器的一个实例,持有 booking ,所以你可以尝试:

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

关于ruby-on-rails - 访问嵌套属性字段的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com