什么是在Rails中检测外部URL的最佳方法?
def external?(url)
return true if url is external
end
def external?(url)
return true if url is external
end
这里没有任何验证或其他操作。如果url
包含根网址,那么它只会返回true。
is_external_url?(url)
true unless url.include?(root_url)
end
def get_host_name(url)
url = "http://#{url}" if URI.parse(url).scheme.nil?
URI.parse(url).host.downcase
end
def is_external_url?(url)
host = get_host_name(url)
host != 'www.mydomain.com'
end
def external?(url)
!!(Rails.application.routes.recognize_path(URI.parse(url).path) rescue false)
end
root_url
指向的是类似于www.mydomain.com/index.html
的东西。你会如何处理呢? - Rajesh Kolappakam