【问题标题】:ActiveShipping and Grape (Rails) - uninitialized constant API::V1::Shippings::ActiveShippingActiveShipping 和 Grape (Rails) - 未初始化的常量 API::V1::Shippings::ActiveShipping
【发布时间】:2015-04-28 09:56:13
【问题描述】:

我使用Grape 在rails 中创建API。现在我想使用 Shopify 的 Active Shipping gem,但是当我尝试在 Grape 中的 custom_helper 中使用它时,我收到了 uninitialized constant ActiveShipping 错误消息。此外,我还收到以下消息:

Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x007f8bea21b4e8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007f8bea21b038 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007f8bec0fe950 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

我的帮助文件如下所示:

require 'active_shipping'
module API
    module V1
        module Helpers
            module ShippingHelpers
                extend Grape::API::Helpers

                def ups_tracking_info(tracking_number)
                    ups = ActiveShipping::UPS.new(login, password, key) #this is where it throws an error
                    ups.find_tracking_info(tracking_number)
                end
            end
        end
    end
end

我将它包含在我的葡萄运输文件中,例如:

module API
  module V1
    class Shippings < Grape::API
        helpers API::V1::Helpers::ShippingHelpers 
       ...
        #some endpoint:
          ups_tracking_info(tracking_info)
       ...
    end
  end
end

为什么我会收到未初始化的常量错误消息?我确实需要我的帮助文件中的 gem.. 在我的 config/application.rb 文件中需要它也不起作用......

希望有人有一个想法,可以进一步帮助我。提前致谢!

请注意,我的所有葡萄文件都在 app/api/api 文件夹中,所以这不是问题..

【问题讨论】:

  • 尝试::ActiveShipping::UPS,以便它在根级别而不是API::V1::Shippings 中查找类。
  • 感谢@Philip 的回答。不幸的是,它没有帮助..请在下面查看我的答案。结果证明文档不正确,我需要在课堂上使用include ActiveMerchant::Shipping

标签: ruby-on-rails grape grape-api uninitialized-constant


【解决方案1】:

原来documentation with the gem on github 不正确。正确的信息写在here。我不得不在课堂上include ActiveMerchant::Shipping 并放弃在代码中使用 ActiveMerchant::Shipping。所以请改用 UPS.new() 或 FedEx.new() 等。

【讨论】:

  • 你也可以不这样做,只做::ActiveMerchant::Shipping::UPS....
  • 我猜,谢谢你的建议! gem 的文档仍然不正确...
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多