【问题标题】:Heroku ActionController::RoutingError (uninitialized constant error)Heroku ActionController::RoutingError(未初始化的常量错误)
【发布时间】:2016-01-19 03:58:07
【问题描述】:

Rails 4.2.4、Ruby 2.1.7

我在 lib/ 目录中有一个模块。

lib/BLL/user_feed.rb

module BLL
 class UserFeed

    def initialize
        logger.debug "Class has been initialized"
    end

    def get_user_feed(user_id)
      # logic here

        return {
         # object
        }
    end
 end
end

当我尝试将其包含在我的控制器中以使用我的 user_Feed 逻辑时,

 class UserfeedController < ApplicationController
  include BLL

 before_action :authenticate_user!

  def show
   # some logic
  end
end

在我的 config/application.rb 中

config.autoload_paths << Rails.root.join('lib')

这在本地运行良好,但是,当我在 Heroku 上部署它时它会中断。

它在扔

ActionController::RoutingError(未初始化常量 UserfeedController::BLL):

错误。

2015-10-20T13:45:13.791457+00:00 app[web.1]: /app/app/controllers/api/v1/userfeed_controller.rb:1:in `<top (required)>': uninitialized constant Bll (NameError)
2015-10-20T13:45:13.791457+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
2015-10-20T13:45:13.791458+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `each'
2015-10-20T13:45:13.791459+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `block in eager_load!'
2015-10-20T13:45:13.791460+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `each'
2015-10-20T13:45:13.791462+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `eager_load!'
2015-10-20T13:45:13.791463+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:346:in `eager_load!'

有什么建议吗?

【问题讨论】:

  • 尝试将文件夹重命名为小写(BLLbll)和(不太可能,前者应该有帮助)include ::BLL
  • 是的,我就是这样。小脑球
  • 查看我的最新回答;可能这会对你有所帮助

标签: ruby-on-rails ruby heroku


【解决方案1】:

我认为您在lib/bll.rb 中缺少module BLL; end

不过,也可以尝试将模块命名为 Bll,但我不认为是这样

【讨论】:

  • 请看@上面的编辑,我添加了错误信息以获取更多信息。
【解决方案2】:

试试

config.autoload_paths += %W(#{config.root}/lib/BLL)

别忘了重启服务器

编辑1
而且;将 Dir BLL 的名称更改为 bll 也可以

【讨论】:

  • 答案已编辑;确保你看过最新的
【解决方案3】:

Ruby 默认为CamelCase,您必须使用以下内容:

#vendor/bll/user_feed.rb
module Bll
  class UserFeed
    ...
  end
end

其次,vendor 目录是自动加载的(据我所知),因此上述代码应该可以修复 UnrecognizedConstant 错误。

https://softwareengineering.stackexchange.com/questions/123305/what-is-the-difference-between-the-lib-and-vendor-folders

【讨论】:

    【解决方案4】:

    Rails 实际上会在应用加载时加载您的应用目录。所以实际上不需要在自动加载路径中提及你的 app/bll

    但是,在这种情况下,我在这里做错的是在类的顶部添加模块。

    所以,我的应用正在寻找 app/bll/bll/Whatever

    module Bll - there is not need for this module to be declared
      class Whatever
        # some logic.
      end
    end
    

    你需要做的就是。

     class Whatever
    
     end
    

    在此之后,您的课程就可以使用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-04
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多