4

Is there an easy way how to validate urls in Rails 4 without using a Gem?

I found the validate_url gem already but i dont actually want to use it, as I think there needs to be a built-in way of doing that

Mik
  • 1,705
  • 1
  • 14
  • 26
  • 1
    Same question is already answered [What's a good way to validate links (urls) in rails 3?][1] [1]: http://stackoverflow.com/questions/7167895/whats-a-good-way-to-validate-links-urls-in-rails-3 – Pravin Mishra Dec 18 '13 at 13:26

2 Answers2

15

I found a good way to do validate an url in rails 4 with a method of the URI class.

class User < ActiveRecord::Base
    validates_format_of :url, :with => URI::regexp(%w(http https))
end
Mik
  • 1,705
  • 1
  • 14
  • 26
5

In addition to @Mik's answer, I also use the browser HTML form:

<%= f.url_field :url %>
Victor
  • 13,010
  • 18
  • 83
  • 146