I have a Ruby on Rails 5.2 app runnng on Ruby 2.6.6 and on the following route: /api/popups it gives me the 500 internal server error.
In the production log I see the following messages:
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'
The controller2.rb index (line 5) looks like:
def index
popups = popups_for_domain.includes(:popup_template, :color_schema, :target_sets)
end
The controller1.rb line 14 contains:
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
The error points to the host function from this line: referer_domain = Addressable::URI.parse(request.referer).host.gsub(/^w{3}\./i, '')
What is wrong there and how can I fix it? Thanks.