Ruby on rails 3.2 accept_nested_attributes验证
问题描述:
我正在研究Rails 3.2应用程序上的ruby,我需要验证带有嵌套属性的窗体。在我的模型我有这个Ruby on rails 3.2 accept_nested_attributes验证
class Member attr_accessible :email, :password.............etc has_many :purchase_informations, :dependent => :destroy accepts_nested_attributes_for :purchase_informations, :allow_destroy => true end class PurchaseInformation belongs_to :member attr_accessor :same_billing end
及以下表单数据:
{ members:
{ purchase_informations_attributes:
{ 0:
{ information_type: "billing",
title: "Mr",
first_name: "",
last_name: "",
cuhk_no: "",
organization: "",
address: "",
zip_code: "",
country: "*",
telephone: "",
mobile: "", fax: "",
email: "asdasdasda", id: "27"
},
1:
{ information_type: "shipping",
title: "Mr",
first_name: "",
last_name: "",
cuhk_no: "",
organization: "",
address: "",
zip_code: "",
country: "*",
telephone: "",
mobile: "",
fax: "",
email: "",
id: "28"
}
}
}
}
请帮我验证这个属性是空白。
谢谢!
答
你好你可以试试这个:
accepts_nested_attributes_for : purchase_informations, :reject_if => :all_blank
或在您的形式把一个:要求=>真正的功能
<%= f.input :name, :required => true %>
嗨艾伦。感谢您的洞察力,不幸的是上面的方法不是一个选项,因为我需要遍历每个值来检查是否有空值。只有所有值都是空白的,它才会起作用。对于输入字段中的必需属性,这用于html 5,并非所有浏览器都支持这一点。非常感谢你。 – Azrael1932 2013-04-24 11:15:02
尝试此操作:此操作验证特定列并验证是否存在嵌套属性的名称: accepting_attributes_for:purchase_informations,:reject_if => lambda {| a | a [:name] .blank? } – 2013-04-25 03:58:29