mercredi 20 septembre 2017

How to validate a object of an object in Rails?

I very new in Rails, but my problem I have is this: I have this class called Url that has it owns validations:

 validates :ipaddress, :format => {
   :with => Resolv::IPv4::Regex,
   :message => 'Deve ser um endereco IP valido'
 }

 validates :domain, :format => {
   :with => URI.regexp,
   :message => 'Deve ser um dominio valido'
 }

I have another class called Link that has an url. The validations of my link class are:

validates :path, :format => {
  :with => /\A[\S]+\Z/,
  :message => 'Deve conter uma path valida'
}

But the problem is when I run this test:

test 'test a link with a valid path' do
  link = Link.new()
  link.url = Url.new()
  link.path = 'this/is/my/favorite/path'
  assert_equal(true, link.valid?, 'Test a valid path')
end

The test says that everything is okay, but the validation of the url is not occurring. How I can force to the Rails understand that the url must be valid too?

Aucun commentaire:

Enregistrer un commentaire