【问题标题】:Ruby on Rails 5.2 - NoMethodError (undefined method `host' for nil:NilClass):Ruby on Rails 5.2 - NoMethodError(nil:NilClass 的未定义方法“主机”):
【发布时间】:2020-12-30 11:20:24
【问题描述】:

我有一个在 Ruby 2.6.6 上运行的 Ruby on Rails 5.2 应用程序,路径如下:/api/popups 它给了我 500 内部服务器错误。

在生产日志中,我看到以下消息:

Started GET "/api/popups" for IP at 2020-12-30 11:12:30 +0000
INFO -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69] Processing by controller2#index as HTML
INFO -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)
FATAL -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69]   
FATAL -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69] NoMethodError (undefined method `host' for nil:NilClass):
FATAL -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69]   
FATAL -- : [e7a305ff-9d4d-4c83-9572-9ea0708e8f69] app/controllers/concerns/controller1.rb:14:in `popups_for_domain'
[e7a305ff-9d4d-4c83-9572-9ea0708e8f69] app/controllers/controller2.rb:5:in `index'

controller2.rb 索引(第 5 行)如下所示:

def index
    popups = popups_for_domain.includes(:popup_template, :color_schema, :target_sets)
end

controller1.rb 第 14 行包含:

def popups_for_domain
    return @popups_for_domain if @popups_for_domain
    referer_domain = Addressable::URI.parse(request.referer).host.gsub(/^w{3}\./i, '')
    @popups_for_domain = Popup.where(domain: referer_domain)
end

错误从这一行指向主机函数:referer_domain = Addressable::URI.parse(request.referer).host.gsub(/^w{3}\./i, '')

那里出了什么问题,我该如何解决?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5 ruby-on-rails-5.2 ruby-on-rails-5.1


    【解决方案1】:

    Addressabel::URI.parse returns nil 当 URL 解析为 nilfalse 时。这意味着——至少有时——你没有request.referer,你也需要处理这些情况。

    这样的事情可能对你有用:

    def popups_for_domain
      return @popups_for_domain if @popups_for_domain
      return unless request.referer      
    
      referer_domain = Addressable::URI.parse(request.referer).host.gsub(/^w{3}\./i, '')
      @popups_for_domain = Popup.where(domain: referer_domain)
    

    结束

    【讨论】:

      【解决方案2】:

      根据:https://stackoverflow.com/a/6880668/1564840

      request.referrer 在最终用户时/可能为空

      - entered the site URL in browser address bar itself.
      - visited the site by a browser-maintained bookmark.
      - visited the site as first page in the window/tab.
      - clicked a link in an external application.
      - switched from a https URL to a http URL.
      - switched from a https URL to a different https URL.
      - has security software installed (antivirus/firewall/etc) which strips the referrer from all requests.
      - is behind a proxy which strips the referrer from all requests.
      - visited the site programmatically (like, curl) without setting the referrer header (searchbots!).
      

      在我的例子中,我直接在浏览器中调用了 API url,这就是缺少引用者的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 2013-03-04
        • 1970-01-01
        • 2022-11-21
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多