0
在我的代碼中,我試圖呼叫url_exist?
作爲網站的驗證,但我不確定如何使用網站的值調用url_exist?
。這似乎是一個愚蠢的問題,但我無法弄清楚。使用參數進行Rails驗證
型號
validates :website, presence: true, if: :url_exist?
...
def url_exist?(url_string)
url = URI.parse(url_string)
puts url
binding.pry
req.use_ssl = (url.scheme == 'https')
req = Net::HTTP.new(url.host, url.port)
path = url.path if url.path.present?
req.request_head(path || '/')
res.code != "404" # false if returns 404 - not found
rescue Errno::ENOENT
false # false if can't find the server
end