Ruby on rails 3.2的Accept_nested_attributes验证

时间:2013-04-24 06:46:15

标签: ruby-on-rails-3.2 nested-forms nested-attributes

我正在使用rails 3.2 app上的ruby,我需要验证具有嵌套属性的表单。在我的模型中我有这个

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: "Hong Kong",
                 telephone: "",
                 mobile: "", fax: "",
                 email: "asdasdasda", id: "27"
              },
           1: 
              { information_type: "shipping",
                title: "Mr",
                first_name: "",
                last_name: "",
                cuhk_no: "",
                organization: "",
                address: "",
                zip_code: "",
                country: "Hong Kong",
                telephone: "",
                mobile: "",
                fax: "",
                email: "",
                id: "28"
              }
         }
     } 
}

请帮我验证属性是否为空白。

谢谢!

1 个答案:

答案 0 :(得分:0)

您好,您可以试试这个:

 accepts_nested_attributes_for : purchase_informations, :reject_if => :all_blank

或在您的表单中填写:required =>真功能

<%= f.input :name, :required => true %>