0
votes

I am working on a ruby on rails 3.2 app and I need to validate a form with nested attributes. In my model I had this

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

and below is the form data:

{ 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"
              }
         }
     } 
}

Please help me out to validate if the attributes are blanks.

Thanks!

1

1 Answers

0
votes

Hi you can try this:

 accepts_nested_attributes_for : purchase_informations, :reject_if => :all_blank

or in your form put a :required => true function

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