Web Cralwers为nil引发了NoMethodError:Geilder的NilClass

时间:2014-07-02 10:55:53

标签: ruby-on-rails ruby web-crawler rails-geocoder

在我向访问者的国家/地区位置进行地理编码的新方法之后,我一直在收到有关我的Rails 4应用程序的NoMethodError报告。

错误报告声明我尝试访问Nil对象的方法

其他信息:

  • Rails 4.0.0
  • Ruby 2.0.0
  • Geocoder 1.1.9

服务器在Nginx& Phusion Passenger

我一直试图修复这个错误一天没有运气。

以下是所涉及方法的片段:

  def set_prefs
    if current_user
      userPrefs = Preflist.find_by_user_id(current_user.id)
      country = Country.find_by_id(userPrefs.country_id)
      session[:pref_country_id] = country.id
      session[:pref_gender] = userPrefs.gender
    else
      # This to make sure that the default @country have an object
      @country = Country.find_by_name('global')

      # Edited if else with unless as suggested by Holger Just
      unless Rails.env.test? || Rails.env.development?
        @geoCountry = Geocoder.search(request.remote_ip).first.country
        unless @geoCountry.blank?
          @newCountry = Country.find_by_name(@geoCountry.downcase())
          unless @newCountry.blank?
            @country = @newCountry
          end
        end
      end

      pref_country_id = session[:pref_country_id]

      if pref_country_id.nil?
        # The only line that request an .id from an Object
        session[:pref_country_id] = @country.id
      end

      pref_gender = session[:pref_gender]

      if pref_gender.nil?
        session[:pref_gender] = 0
      end

    end
end

以下是完整报告的示例

A NoMethodError occurred in things#map:
undefined method `id' for nil:NilClass
app/controllers/things_controller.rb:174:in `set_prefs'
# The reported line also keeps on changing, not just 174, which is kind of odd

-------------------------------
Request:
-------------------------------
* URL        :
* HTTP Method: GET
* IP address : 66.249.77.53
* Parameters :
* Timestamp  : 2014-07-02 16:42:38 +0800
* Server :
* Rails root :
* Process: 12911
-------------------------------
Session:
-------------------------------
* session id: [FILTERED]
* data: #<ActionDispatch::Request::Session:0x422f570 not yet loaded>
-------------------------------
Environment:
-------------------------------
* DOCUMENT_ROOT                                  :
* HTTPS                                          : on
* HTTP_ACCEPT                                    : */*
* HTTP_ACCEPT_ENCODING                           : gzip,deflate
* HTTP_CONNECTION                                : Keep-alive
* HTTP_FROM                                      : googlebot(at)googlebot.com
* HTTP_HOST                                      :
* HTTP_USER_AGENT                                : Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
* ORIGINAL_FULLPATH                              :
* ORIGINAL_SCRIPT_NAME                           : 
* PASSENGER_APP_ENV                              : production
* PASSENGER_APP_TYPE                             : rack
* PASSENGER_CONNECT_PASSWORD                     : [FILTERED]
* PATH_INFO                                      :
* QUERY_STRING                                   :
* REMOTE_ADDR                                    : 66.249.77.53
* REMOTE_PORT                                    : 42866
* REQUEST_METHOD                                 : GET
* REQUEST_URI                                    :
* ROUTES_33552780_SCRIPT_NAME                    : 
* SCGI                                           : 1
* SCRIPT_NAME                                    : 
* SERVER_ADDR                                    :
* SERVER_NAME                                    :
* SERVER_PORT                                    : 443
* SERVER_PROTOCOL                                : HTTP/1.1
* SERVER_SOFTWARE                                : nginx/1.4.4
* action_controller.instance                     : #<ThingsController:0x000000042fdb50>
* action_dispatch.backtrace_cleaner              : #<Rails::BacktraceCleaner:0x00000003b86fc0>
* action_dispatch.cookies                        : #<ActionDispatch::Cookies::CookieJar:0x00000004384768>
* action_dispatch.encrypted_cookie_salt          : encrypted cookie
* action_dispatch.encrypted_signed_cookie_salt   : signed encrypted cookie
* action_dispatch.http_auth_salt                 : http authentication
* action_dispatch.key_generator                  : #<ActiveSupport::CachingKeyGenerator:0x00000003cf5348>
* action_dispatch.logger                         : #<Logger:0x00000004071c60>
* action_dispatch.parameter_filter               : [:password]
* action_dispatch.redirect_filter                : []
* action_dispatch.remote_ip                      : 66.249.77.53
* action_dispatch.request.accepts                : [#<Mime::Type:0x000000071052f0 @synonyms=[], @symbol=nil, @string="*/*">]
* action_dispatch.request.content_type           : 
* action_dispatch.request.formats                : [#<Mime::Type:0x000000071052f0 @synonyms=[], @symbol=nil, @string="*/*">]
* action_dispatch.request.parameters             :
* action_dispatch.request.path_parameters        :
* action_dispatch.request.query_parameters       :
* action_dispatch.request.request_parameters     : {}
* action_dispatch.request.unsigned_session_cookie: {}
* action_dispatch.request_id                     : dd13eb0d-dbc9-4093-b7bc-013208ba7dc4
* action_dispatch.routes                         : #<ActionDispatch::Routing::RouteSet:0x00000003fff318>
* action_dispatch.secret_key_base                : 162e9bbc51d7fc7931ade00f2184de5bc3533b3f19d02bef4a2e30e8e67fb8fbe29e6655baa8033ca9fc1a591931c4964d413d57dc825d6bbae9c7e67293f576
* action_dispatch.secret_token                   : 
* action_dispatch.show_detailed_exceptions       : false
* action_dispatch.show_exceptions                : true
* action_dispatch.signed_cookie_salt             : signed cookie
* rack.errors                                    : #<IO:0x00000000aae178>
* rack.hijack                                    : #<Proc:0x000000041b8858@/home/deploy/.rvm/gems/ruby-2.0.0-p353/gems/passenger-4.0.35/lib/phusion_passenger/rack/thread_handler_extension.rb:69 (lambda)>
* rack.hijack?                                   : true
* rack.input                                     : #<PhusionPassenger::Utils::TeeInput:0x00000004186588>
* rack.multiprocess                              : true
* rack.multithread                               : false
* rack.request.cookie_hash                       : {}
* rack.request.query_hash                        :
* rack.request.query_string                      : 
* rack.run_once                                  : false
* rack.session                                   : #<ActionDispatch::Request::Session:0x0000000422f570>
* rack.session.options                           : #<ActionDispatch::Request::Session::Options:0x0000000422f098>
* rack.url_scheme                                : https
* rack.version                                   : [1, 2]

-------------------------------
Backtrace:
-------------------------------
app/controllers/things_controller.rb:174:in `set_prefs'

1 个答案:

答案 0 :(得分:0)

经过进一步调查,该错误已得到修复。

每隔5分钟发布的错误报告是由于gem&#39; exception_notification&#39;无法一次发送多个报告,所以即使在晚些时候说午夜12点,它仍然会发送错误报告,发生在下午4点。

谢谢大家,我为这种困惑道歉。