5

I have a custom class stored in /lib (/lib/buffer_app.rb):

require 'HTTParty'

class BufferApp
  include HTTParty
  base_uri 'https://api.bufferapp.com/1'

  def initialize(token, id)
    @token = token
    @id = id
  end

  def create(text)
    message_hash = {"text" => text, "profile_ids[]" => @id, "access_token" => @token}

    response = BufferApp.post('/updates/create.json', :body => {"text" => text, "profile_ids[]" => @id, "access_token" => @token})
  end
end

I'm attempting to use this this class in an Active Admin resource and get the following error when in production (Heroku):

NameError (uninitialized constant Admin::EventsController::BufferApp):

It's worth noting I have this line in my application.rb and that this functionality works locally in development:

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

If I try include BufferApp or require 'BufferApp' that line itself causes an error. Am I having a namespace issue? Does this need to be a module? Or is it a simple configuration oversight?

4

3 回答 3

7

我在 Rails 5 alpha 中遇到了完全相同的问题。为了解决这个问题,我不得不手动要求文件:

require 'buffer_app'

而不是: ( require 'BufferApp')

即使 Michal Szyndel 的回答对我来说很有意义,在手动要求文件之后,::在我的情况下,为常量添加前缀也没有影响。

无论如何,我对需要解决方案的手册不满意,因为我需要添加特定于环境的代码。为什么我不需要在开发中手动要求文件?

于 2015-09-22T17:50:03.323 回答
5

改变这个

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

对此

config.eager_load_paths += %W(#{Rails.root}/lib)

eager_load_paths将在生产和开发中按需加载。这样做,您不需要明确要求每个文件。

查看有关此答案的更多信息。

于 2017-05-04T01:48:17.223 回答
3

错误行说明了一切,您应该将类​​引用为::BufferApp

于 2013-06-20T13:54:01.810 回答