How can I get this to work? Use !!URI.parse(url) or self.url ?
class Idea < ActiveRecord::Base
attr_accessible :body, :url
validates :body, presence: true, :length => { :in => 6..240 }
validates :body, uniqueness: true
validates :url, presence: true
validate :good_url
require 'uri'
def good_url
errors.add(:url, 'not valid') unless URI.parse(url)
end
end
If you just want to get it working, then how about:
But if parsing urls is important to you, you might want to consider an alternative to ruby’s standard URI implementation such as addressable, which handles UTF-8 characters, normalization and other edge cases that might be important depending on the context.
See also this: check if url is valid ruby