Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Rails Quick Tip: URL Validation for ActiveRecord

Posted on 2018-08-29 by Rodrigo Urubatan

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.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme