相同的控制器和库类名

时间:2013-09-29 08:22:50

标签: ruby-on-rails

有两个相似的类名(一个在lib中,一个在控制器中)是错误的吗?

的myapp / LIB /公司/ api.rb

module Company
  class Api
    include HTTParty

    base_uri "#{API_CONFIG['scheme']}://#{API_CONFIG['host']}"
    digest_auth API_CONFIG['email'], API_CONFIG['key']

    DEFAULT_OPTIONS = {
      limit: 1
    }

  #...
end

MyApp的/应用/控制器/ API / something_controller.rb

class Api::SomethingController < ApplicationController
  def index
    # TODO: Verify q
    q = params[:q]

    @result = Company::Api.new().send("get_sdf_#{q}",
                                   rn: "123",
                                   limit: params[:limit])


  #...
end

development.log

LoadError (Expected /home/cekpo/myapp/lib/company/api.rb to define Api):
  activesupport (3.2.13) lib/active_support/dependencies.rb:503:in `load_missing_constant'
  activesupport (3.2.13) lib/active_support/dependencies.rb:192:in `block in const_missing'
  activesupport (3.2.13) lib/active_support/dependencies.rb:190:in `each'
  activesupport (3.2.13) lib/active_support/dependencies.rb:190:in `const_missing'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:230:in `block in constantize'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `each'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `constantize'
  activesupport (3.2.13) lib/active_support/dependencies.rb:554:in `get'

1 个答案:

答案 0 :(得分:0)

这里没有两个相同的命名类。一个类名为Api,在模块Company中定义。另一个是在模块SomethingController中定义的类Api

我想你忘了定义模块Company

添加名为lib/Company.rb且内容为

的文件
module Company
end

现在应该可以了。