I did a quick search, and didn’t find an easy way to validate URL fields.
I tried writing my own regular expressions but they kept getting more complex every time, and I do not really like to write complex regexps (If I liked it I would be programming in PERL π )
So I decided to think a little, because the class URI already knows if something is a valid URI, and it probably exposes some really useful method for that.
And I came up with this simple code:
class Document < ApplicationRecord validates :address, format: {with: URI::regexp(%w(http https))} endΒ
So I’m writing this post here so I can find how to do it next time I need, and I hope it can be more useful for more people.