Here's a quick-and-dirty way to validate URLs in your model. Updated: Only allow certain schemes.
require 'uri'
class Film < ActiveRecord::Base
VALID_URI_SCHEMES = ['http', 'https']
validates_presence_of :url
validate :url_must_be_valid
protected
def url_must_be_valid
parsed = URI.parse(url)
if !VALID_URI_SCHEMES.member?(parsed.scheme)
raise URI::InvalidURIError
end
rescue URI::InvalidURIError => e
errors.add(:url, 'is not a valid URL')
end
end
Comments
require 'test_helper'
class MyModelTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
test "url_must_be_valid catches bad urls" do
["*://", "ftp://example.com/foo.jpg"].each do |url|
my_model = MyModel.new
my_model.name = "name"
my_model.url = url
assert !my_model.valid?, url
end
end
test "url_must_be_valid accepts good urls" do
my_model = MyModel.new
my_model.name = "name"
my_model.url = "http://example.com/foo.jpg"
assert my_model.valid?, my_model.errors.full_messages
end
end
URL_FORMAT = URI::regexp(%w(http https))
validates_format_of :url, :with => URL_FORMAT