ActiveResource帖子引发错误,而在我的浏览器中发布帖子成功

时间:2014-08-31 14:15:00

标签: ruby-on-rails mongodb activeresource

我有一个非常奇怪的问题,我无法解决。两个Rails应用程序,一个是具有实体Mongo :: Enrollment的API,另一个是具有通过ActiveResource连接到API的实体Enrollment的客户端。

我使用ActiveResource在客户端上定义了我的/app/models/enrollment.rb,在控制器中我正在进行这样的调用:

enrollment = Enrollment.post(:create, enrollment: {
        :enrollment_type => :test,
        :enrollment_information => {test: 'test information'}
})

我知道我可以在这里使用.new和.save,因为它是ActiveResource,但它给了我相同的结果。

当我实际发布此消息时,我的日志中出现以下错误:

RuntimeError (Circular dependency detected while autoloading constant Enrollment)

但是如果我从我的日志中复制URL,使用相同的Basic身份验证,接受标头和POST方法将其粘贴到Postman中,我根本没有错误,它会将注册保存到mongodb。

我真的希望有人可以帮助我。我认为看起来完全相同的HTTP请求非常奇怪,取得了不同的结果。我尝试重新启动这两个应用程序,并以不同的方式从客户端执行请求,但每次我从客户端发出请求时,它都会给我上述错误。 API端的控制器和模型看起来很好,我在API应用程序上也有一个HTML界面,并且工作正常。

这就是我的API应用程序中的内容:

它在路由中定义为:

resources :enrollments

在EnrollmentsController中我有

# /app/controllers/enrollments_controller.rb
class EnrollmentsController < ApplicationController
  ...
  def create

    @enrollment = Mongo::Enrollment.new(enrollment_parameters)

    respond_to do |format|
      if @enrollment.save
        format.html { redirect_to @enrollment, notice: 'Note was successfully created.' }
        format.json { render json: @enrollment }
      else
        format.html { render action: 'new' }
        format.json { render json: @enrollment.errors, status: :unprocessable_entity }
      end
    end

  end
  ...
end

这是我的模特:

# /app/models/mongo/enrollment.rb
module Mongo
  class Enrollment
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Paranoia

    default_scope ->{ where(processed: false)}

    field :processed, :type => Boolean, :default => false
    field :accepted, :type => Boolean, :default => false

    field :enrollment_type

    field :enrollment_information, :type => Hash, :default => {}
  end
end

完整堆栈跟踪:

Started POST "/api/v1.0/enrollments/create.json?enrollment..." for 127.0.0.1 at 2014-08-31 10:12:33 -0400

RuntimeError (Circular dependency detected while autoloading constant Enrollment):
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:461:in `load_missing_constant'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:184:in `const_missing'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `const_get'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `block in constantize'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `each'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `inject'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `constantize'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:269:in `safe_constantize'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/core_ext/string/inflections.rb:77:in `safe_constantize'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:145:in `_default_wrap_model'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:96:in `block in model'
  /usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
  /usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/mutex_m.rb:73:in `mu_synchronize'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:96:in `model'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:119:in `name'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:252:in `_wrapper_key'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:275:in `_wrapper_enabled?'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/params_wrapper.rb:233:in `process_action'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/abstract_controller/base.rb:136:in `process'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/abstract_controller/rendering.rb:44:in `process'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal.rb:195:in `dispatch'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_controller/metal.rb:231:in `block in action'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:80:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:48:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:71:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:59:in `each'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/journey/router.rb:59:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:680:in `call'
  vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
  vendor/bundle/ruby/2.1.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/etag.rb:23:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/conditionalget.rb:35:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/flash.rb:241:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/cookies.rb:486:in `call'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/query_cache.rb:36:in `call'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:369:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__928559686495229391__call__callbacks'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/reloader.rb:64:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:38:in `call_app'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:20:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:67:in `block in tagged'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:25:in `tagged'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/tagged_logging.rb:67:in `tagged'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/rack/logger.rb:20:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/static.rb:64:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/engine.rb:511:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:97:in `call'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.48/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'


  Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
  Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.8ms)
  Rendered vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (20.3ms)

谢谢!

1 个答案:

答案 0 :(得分:0)

听起来wrap_parameters代码被您的模型在Mongo模块中命名空间这一事实所困惑。

听起来你实际上并不需要参数包装,所以最简单的方法就是禁用它 - 应该有一个wrap_parameters初始化器。如果需要,您可以在控制器的基础上在控制器上重新启用它。