我使用 Airbrake 通知我有关 Spree Commerce 商店中的错误。现在我想使用spree的API,当我尝试发布到http://localhost:3000/api/products以创建新产品时出现以下错误:

NoMethodError (undefined method `airbrake_request_data' for #<Spree::Api::V1::ProductsController:0x007f9fcdb81b58>):
  activesupport (3.2.9) lib/active_support/core_ext/object/try.rb:36:in `try'
  airbrake (3.1.11) lib/airbrake/rails/middleware.rb:46:in `request_data'
  airbrake (3.1.11) lib/airbrake/rails/middleware.rb:39:in `notify_airbrake'
  airbrake (3.1.11) lib/airbrake/rails/middleware.rb:15:in `rescue in call'
  airbrake (3.1.11) lib/airbrake/rails/middleware.rb:12:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  airbrake (3.1.11) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (3.1.11) lib/airbrake/user_informer.rb:12:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392@system/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392@system/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (16.6ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392@system/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (55.2ms)

显然,我的调用中有错误。但我想知道哪个错误。当然,Airbrake 也应该从 API 中通知错误。我使用 Spree 配置了 Airbrake,因此:
Gemfile
gem "airbrake"
initializers/airbrake.rb
Airbrake.configure do |config|
  config.api_key = 'XXXXXXXXXXXXXXXXXXX'
end

所以,一个简单的设置。我正在使用 Spree 1-2-stable,我不知道从哪里开始解决这个问题。

最佳答案

您遇到此错误是因为 Spree API 扩展了 ActionController::Metal 而不是 ActionController::Base。

airbrake gem 执行此处显示的一些逻辑:
https://github.com/airbrake/airbrake/blob/68543097a8731cf1f3e717946bd5ed33e5edccad/lib/airbrake/rails.rb#L13

其中包括 ActionController::Base 中的方法。

关于如何解决这个问题,您有几个选择:

  • 使用与 airbrake gem 中显示的方法类似的方法将方法加载到 ActionController::Metal 中
  • 使用类似的方法将方法加载到 Spree::Api::BaseController
  • 装饰 Spree::Api::BaseController 以直接包含方法

  • 那应该能让你继续前进。

    关于ruby-on-rails - Airbrake 和 Spree 商务 API : undefined method `airbrake_request_data` ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16884268/

    10-12 03:00