Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 指向解释为相对URL的用户指定URL的链接_Ruby On Rails_Url_User Input_Url Parsing - Fatal编程技术网

Ruby on rails 指向解释为相对URL的用户指定URL的链接

Ruby on rails 指向解释为相对URL的用户指定URL的链接,ruby-on-rails,url,user-input,url-parsing,Ruby On Rails,Url,User Input,Url Parsing,很抱歉,如果这个问题已经在别处得到了回答,但我花了一段时间寻找,没有运气 在我的web应用程序中,我要求用户指定其博客的URL。但是,他们并不总是在这些URL的开头加上“http://”。在站点的其他地方,当我链接到这些URL时,浏览器将它们解释为相对URL。e、 g.如果用户编写bobsblog.wordpress.com,则链接将转到 一种解决方案是用“http://”预先填充url字段 但更好的解决方案是解析url,如果用户没有添加方案,则添加方案。rails是否提供了一种很好的方法来实现

很抱歉,如果这个问题已经在别处得到了回答,但我花了一段时间寻找,没有运气

在我的web应用程序中,我要求用户指定其博客的URL。但是,他们并不总是在这些URL的开头加上“http://”。在站点的其他地方,当我链接到这些URL时,浏览器将它们解释为相对URL。e、 g.如果用户编写bobsblog.wordpress.com,则链接将转到

一种解决方案是用“http://”预先填充url字段


但更好的解决方案是解析url,如果用户没有添加方案,则添加方案。rails是否提供了一种很好的方法来实现这一点?我查看了函数URI::parse,但它似乎没有提供一种很好的方法来实现这一点。

也许在您的模型中,您可以有一种方法将URL作为绝对URL返回。如果它不是以“http://”开头,只需将其添加到前面即可

def abs_url
    (url.start_with? "http://") ? url : ("http://" + url)
end
在您看来,只需执行类似于
@user.abs\uURL
的操作即可


编辑:哦,我不知道,但你可能想在提交时这样做。在保存之前,可以执行类似的逻辑。在这种情况下:

before_save :abs_url
...
def abs_url
    url = (url.start_with? "http://") ? url : ("http://" + url)
end

URL将与前面的“http://”一起保存。

您可以使用URI.parse并检查方案

def abs_url
    (url.start_with? "http://") ? url : ("http://" + url)
end
before_save :sanitize_url

def sanitize_url
  uri = URI.parse(url)
  self.url = "http://#{url}" if uri.scheme.blank?
rescue URI::InvalidURIError => e
  # not a parseable URI, so you need to handle that
end
这里有一些输出

ree-1.8.7-2011.03 :035 > x = URI.parse "http://google.com"
 => #<URI::HTTP:0x1069720c8 URL:http://google.com> 
ree-1.8.7-2011.03 :036 > x.scheme
 => "http" 
ree-1.8.7-2011.03 :037 > y = URI.parse "google.com"
 => #<URI::Generic:0x1069672e0 URL:google.com> 
ree-1.8.7-2011.03 :038 > y.scheme
 => nil 
ree-1.8.7-2011.03 :039 > z = URI.parse "https://google.com"
 => #<URI::HTTPS:0x10695b8f0 URL:https://google.com> 
ree-1.8.7-2011.03 :040 > z.scheme
 => "https" 
ree-1.8.7-2011.03:035>x=URI.parse“http://google.com"
=> # 
ree-1.8.7-2011.03:036>x方案
=>“http”
ree-1.8.7-2011.03:037>y=URI.parse“google.com”
=> # 
ree-1.8.7-2011.03:038>y.方案
=>零
ree-1.8.7-2011.03:039>z=URI.parse“https://google.com"
=> # 
ree-1.8.7-2011.03:040>z方案
=>“https”

非常感谢!我还刚刚想到,以“https://”开头的URL也可以正常使用(不需要http://前缀)。