0

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 Answer 1

0

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 %>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Allen. Thanks for your insight, unfortunately above method is not an option since I need to iterate through each values to check if there is blank values. It will only work if all values are blank. For the required attribute on the input field, this is used on html 5 and not all browsers support this. Thank you very much.
Try this: this validates for a specific column and validates presence of a name for a nested attribute: accepts_nested_attributes_for : purchase_informations, :reject_if => lambda { |a| a[: name].blank? }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.