Heroku + Rails + GeoIp = 未初始化的常量 GeoIP 错误

Posted

技术标签:

【中文标题】Heroku + Rails + GeoIp = 未初始化的常量 GeoIP 错误【英文标题】:Heroku + Rails + GeoIp = uninitialized constant GeoIP error 【发布时间】:2013-05-02 13:30:39 【问题描述】:

我正在尝试将我的旧网站部署到 Heroku。 一切顺利,它开始了,但是当我通过网络浏览器访问它时,我得到了

NameError (uninitialized constant ApplicantsController::GeoIP):

我使用的是 Rails 2.3.18(我的网站非常简单,只想“按原样”尽快部署它;在专用服务器上一切正常)。

我在“db”文件夹和 controllers/applicants_controller.rb 中有一个 GeoIP.dat 文件

line 32:    geoip ||= GeoIP.new("db/GeoIP.dat")
line 33:    location = geoip.country('google.com')

如果我在 Heroku 上启动 irb,一切看起来都不错:

login@sancho:~$ heroku run bash
Running `bash` attached to terminal... up, run.7785
~ $ irb
irb(main):001:0> require 'geoip'
=> true
irb(main):002:0> geoip ||= GeoIP.new("db/GeoIP.dat")
=> #<GeoIP:0x00000002b3a6a8 @mutex=#<Mutex:0x00000002b3a680>, @flags=0, @database_type=1, @record_length=3, @file=#<File:db/GeoIP.dat>, @database_segments=[16776960]>
irb(main):004:0> geoip.country('google.com')
=> #<struct GeoIP::Country request="google.com", ip="74.125.228.110", country_code=225, country_code2="US", country_code3="USA", country_name="United States", continent_code="NA">
irb(main):005:0> 

这是完整的错误日志。

2013-05-02T13:08:25.938481+00:00 app[web.1]: Processing ApplicantsController#create (for 87.207.27.219 at 2013-05-02 13:08:25) [POST]
2013-05-02T13:08:25.938481+00:00 app[web.1]:   Parameters: "status"=>"complete"
2013-05-02T13:08:26.029730+00:00 app[web.1]: 
2013-05-02T13:08:26.029730+00:00 app[web.1]: NameError (uninitialized constant ApplicantsController::GeoIP):
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.18/lib/active_support/dependencies.rb:131:in `rescue in const_missing'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/filters.rb:610:in `perform_action_with_filters'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/benchmarking.rb:68:in `block in perform_action_with_benchmark'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/rescue.rb:160:in `perform_action_with_rescue'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/flash.rb:151:in `perform_action_with_flash'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/dispatcher.rb:87:in `dispatch'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/dispatcher.rb:121:in `_call'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/query_cache.rb:29:in `block in call'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/session/cookie_store.rb:99:in `call'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   <internal:prelude>:10:in `synchronize'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.18/lib/active_support/dependencies.rb:120:in `const_missing'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/base.rb:532:in `process'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/dispatcher.rb:130:in `block in build_middleware_stack'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   app/controllers/applicants_controller.rb:32:in `create'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/base.rb:391:in `process'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/filters.rb:617:in `call_filters'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/base.rb:386:in `call'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/base.rb:1333:in `perform_action'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/filters.rb:606:in `process_with_filters'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/query_cache.rb:29:in `call'
2013-05-02T13:08:26.029730+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/benchmarking.rb:68:in `perform_action_with_benchmark'
2013-05-02T13:08:26.030057+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/routing/route_set.rb:438:in `call'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/query_cache.rb:9:in `cache'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activerecord-2.3.18/lib/active_record/query_cache.rb:28:in `call'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/params_parser.rb:15:in `call'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/connection.rb:79:in `catch'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/string_coercion.rb:25:in `call'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/connection.rb:79:in `pre_process'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/server.rb:159:in `start'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/connection.rb:54:in `process'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/backends/base.rb:63:in `start'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/runner.rb:187:in `run_command'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/connection.rb:81:in `block in pre_process'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/runner.rb:152:in `run!'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/dispatcher.rb:106:in `call'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/bin/thin:6:in `<top (required)>'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
2013-05-02T13:08:26.030520+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/connection.rb:39:in `receive_data'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/bin/thin:19:in `load'
2013-05-02T13:08:26.030298+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/actionpack-2.3.18/lib/action_controller/failsafe.rb:26:in `call'
2013-05-02T13:08:26.030750+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/thin-1.5.1/lib/thin/controllers/controller.rb:86:in `start'
2013-05-02T13:08:26.030750+00:00 app[web.1]: Rendering /app/public/500.html (500 Internal Server Error)

来自 Heroku 的捆绑日志:

   Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
   The source :gemcutter is deprecated because HTTP requests are insecure.
   Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
   Using rake (0.8.7)
   Using activesupport (2.3.18)
   Using rack (1.1.6)
   Using actionpack (2.3.18)
   Using actionmailer (2.3.18)
   Using activerecord (2.3.18)
   Using activeresource (2.3.18)
   Using daemons (1.1.9)
   Using eventmachine (1.0.3)
   Using geoip (1.2.1)
   Using pg (0.15.1)
   Using rails (2.3.18)
   Using thin (1.5.1)
   Using bundler (1.3.2)
   Your bundle is complete! It was installed into ./vendor/bundle

提前感谢您的帮助!

【问题讨论】:

【参考方案1】:

问题解决了。

我忘记编辑 config/boot.rb 并按照此处所述创建 edit/preinitializer.rb http://gembundler.com/v1.3/rails23.html

【讨论】:

以上是关于Heroku + Rails + GeoIp = 未初始化的常量 GeoIP 错误的主要内容,如果未能解决你的问题,请参考以下文章

Rails GeoLiteCity GeoIP 在本地开发环境中不起作用

Heroku、GeoIP、亚马逊 Route 53

Rails GeoIP 返回国家/城市列表

Rails - 我必须在我的 rails 项目中存储数据文件 (.dat) 的地方 - GeoIp City 数据库

在 Heroku 中安装 C 库

Heroku / Rails:PG :: Undefined Table:错误“[tablename]”在heroku rails迁移上不存在