【问题标题】:creating a helper method to remove http or https from the beginning of a url in rails创建一个辅助方法以从 Rails 中的 url 的开头删除 http 或 https
【发布时间】:2013-05-06 18:29:09
【问题描述】:

我接受网络链接形式的用户输入,例如:http://google.com

我不想在我的数据库中存储http:// 前缀或https://。我打算在 URL 的开头对这两件事进行字符串搜索。

我觉得这是 rails/ruby 开箱即用的东西,有人知道这样的功能吗?

【问题讨论】:

  • 我认为this 可能会有所帮助。

标签: ruby-on-rails ruby ruby-on-rails-3 helper


【解决方案1】:

来自document

require 'uri'

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
uri.scheme   #=> "http"
uri.host     #=> "foo.com"
uri.path     #=> "/posts"
uri.query    #=> "id=30&limit=5"
uri.fragment #=> "time=1305298413"

或者,

require 'uri'

URI.split("http://www.ruby-lang.org/")
# => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]

【讨论】:

  • @sawa fwiw 在您的最后一个示例中注意到第一个“?”维护功能查询参数所必需的已删除。
  • @seanomlor 你是对的。我只是删除了这个例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 2016-07-18
  • 1970-01-01
  • 2015-05-27
  • 2017-11-19
  • 2020-06-22
相关资源
最近更新 更多