Ruby on Rails 5.2 - NoMethodError(nil:NilClass 的未定义方法“主机”):
Ruby on Rails 5.2 - NoMethodError (undefined method `host' for nil:NilClass):
我在 Rails 5.2 上有一个 Ruby 应用程序在 Ruby 2.6.6 和以下路线上运行:/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, '')
那里出了什么问题,我该如何解决?谢谢。
Addressabel::URI.parse
returns nil
当 URL 解析为 nil
或 false
时。这意味着 - 至少有时 - 你没有 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)
结束
根据:
request.referrer will/may当enduser:
为空
- 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,这就是引荐来源网址丢失的原因。
我在 Rails 5.2 上有一个 Ruby 应用程序在 Ruby 2.6.6 和以下路线上运行:/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, '')
那里出了什么问题,我该如何解决?谢谢。
Addressabel::URI.parse
returns nil
当 URL 解析为 nil
或 false
时。这意味着 - 至少有时 - 你没有 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)
结束
根据:
request.referrer will/may当enduser:
为空- 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,这就是引荐来源网址丢失的原因。