指向用户指定 url 的链接被解释为相对 url

发布于 2024-12-28 15:26:32 字数 447 浏览 3 评论 0原文

很抱歉,如果这个问题已在其他地方得到解答,但我花了一段时间寻找但没有运气。

在我的网络应用程序中,我要求用户指定其博客的网址。但是,他们并不总是将“http://”放在这些网址的开头。在网站的其他地方,当我链接到这些网址时,浏览器会将它们解释为相对网址。例如,如果用户写 bobsblog.wordpress.com,则链接将转到 http://www.mydomain。 com/bobsblog.wordpress.com

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

但更好的解决方案是解析 url 并添加方案(如果用户没有)。 Rails 是否提供了一个好方法来做到这一点?我查看了 URI::parse 函数,但它似乎没有提供一个好的方法来做到这一点。

Sorry if this question has been answered elsewhere, but I've spent awhile looking with no luck.

In my web app, I ask users to specify urls to their blogs. However, they don't always put "http://" at the beginning of these urls. Elsewhere on the site, when I link to these urls, the browser interprets them as relative urls. e.g. if the user writes bobsblog.wordpress.com, the link goes to http://www.mydomain.com/bobsblog.wordpress.com.

One solution is to pre-populate the url field with "http://".

But a nicer solution would be to parse the url and add the scheme if the user hasn't. Does rails offer a good way to do this? I looked at the function URI::parse, but it doesn't seem to offer a good way of doing that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2025-01-04 15:26:32

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

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" 

You could use URI.parse and check the scheme.

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

Here some output

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" 
诗笺 2025-01-04 15:26:32

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

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

在您看来,只需执行类似 @user.abs_url 的操作即可。


编辑:哦,我没有意识到,但您可能想在提交时执行此操作。保存之前可以完成类似的逻辑。在这种情况下:

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

并且 URL 将以前面的“http://”保存。

Maybe in your model, you could have a method to return the URL as an absolute URL. If it doesn't start with "http://", just add it to the front.

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

And in your view, just do something like @user.abs_url.


Edit: Oh, I didn't realize, but you probably wanted to do this upon submission. Similar logic can be done before the save. In that case:

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

And the URL will be saved with the "http://" in the front.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文