I would like to make sure that my class has a urlproperty, and if so, it really is:
class Entity < ActiveRecord::Base
validates :name, presence: true
validates :url, presence: true, :format => {:with => URI.regexp}
end
In the rails console:
> e = Entity.new(name: 'foo')
=>
This results in two errors for the attribute url:
> e.valid?
=> false
> e.errors
=> #<ActiveModel::Errors:0x007fed9e324e28 @base=#<Entity id: nil, name: "foo", url: nil, created_at: nil, updated_at: nil>, @messages={:url=>["can't be blank", "is invalid"]}>
Ideally, a nil urlwill create a single error (i.e. can't be blank).
So I changed the rule validates:
validates :url, presence: true, :with => Proc.new { URI.regexp if :url? }
I cannot get the syntax to work. What am I missing?
source
share