I am just going through the book and I come across a problem with the
code in the app/models:
validates_format_of :image_url,
:with => %r{^http:.+\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image"
It seems to moan when it’s empty even though I removed :image_url from
validates_presence_of. How should I make it optional? Altering the
regex?
Btw I have the same sort of problem with example code for the price
example. I want it optional unless I used validates_presence_of. Isn’t
that sensible?
regex?
u.password.size > 0}
Eh? I don’t need confirmation. I just want to make :image_url an
optional requirement in the form, as well as checking input IF
:image_url is entered.
I am just going through the book and I come across a problem with the
code in the app/models:
validates_format_of :image_url,
:with => %r{^http:.+\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image"
It seems to moan when it’s empty even though I removed :image_url from
validates_presence_of. How should I make it optional? Altering the
regex?
Btw I have the same sort of problem with example code for the price
example. I want it optional unless I used validates_presence_of. Isn’t
that sensible?
In the SQL DDL, does NOT NULL mean anything here?
This works for confirmation and I guess it would work in your case too
He’s pointing out that you can pass it an :if option… So you’d want
to do something like
validates_format_of :image_url,
:with => %r{^http:.+\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image",
:if => Proc.new { |m| !m.image_url.nil? and m.image_url.length
0 }
Not sure if that’ll do it, but hopefully it gets you going on the
right path. Check out the API docs for the validation methods.
It seems to moan when it’s empty even though I removed :image_url from
right path. Check out the API docs for the validation methods.
that was pointless in the book…I ended up just changing the line
:with => %r{^http:.+.(gif|jpg|png)$}i,
to
:with => %r{^.+.(gif|jpg|png)$}i,
and then I could move on because the http directive isn’t going to work
unless you put the files on a web server - pointless for the
demonstration and that is a flaw in the book.
Craig
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.