0
我怎樣才能使這個工作?使用!! URI.parse(url)或self.url?Rails 3.2中的自定義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
這裏是解決同一個問題http://stackoverflow.com/questions/7167895/whats-a-good-way-to-validate-links-urls-in-問題rails-3 –
[更新的UriValidator](http://stackoverflow.com/a/19423623/356895) – JJD