Devise中的ArgumentError :: RegistrationsController #create,没有应用程序跟踪或参数

时间:2013-03-03 16:50:29

标签: ruby-on-rails devise

我发现这很奇怪,所以我希望有人可以帮助我。我正在使用Devise,我的用户模型定义如下:

class User < ActiveRecord::Base
    devise :database_authenticatable,
        :registerable,
        :token_authenticatable,
        :recoverable,
        :rememberable,
        :trackable,
        :validatable

        # Setup accessible (or protected) attributes for your model
        attr_accessible :email, :password, :password_confirmation
        attr_accessible :authentication_token
    en

当我尝试注册新用户时,出现以下错误:

ArgumentError

这不是很有用,因为它不提供任何其他信息。

以下是参数:

{"utf8"=>"✓",
"authenticity_token"=>"9VwmXYwBHEvdDxuPmyHu6tjcKeM6QoBtz6Y4ipBTtjQ=",
"user"=>{"email"=>"jeremy@[redacted].ca",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}

下面是堆栈跟踪,希望其他人可以理解它。

bcrypt-ruby (3.0.1) lib/bcrypt.rb:160:in `create'
devise (2.2.3) lib/devise/models/database_authenticatable.rb:110:in `password_digest'
devise (2.2.3) lib/devise/models/database_authenticatable.rb:37:in `password='
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.12) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.12) lib/active_record/base.rb:497:in `initialize'
devise (2.2.3) lib/devise/models/registerable.rb:20:in `new'
devise (2.2.3) lib/devise/models/registerable.rb:20:in `new_with_session'
devise (2.2.3) app/controllers/devise/registrations_controller.rb:87:in `build_resource'
devise (2.2.3) app/controllers/devise/registrations_controller.rb:13:in `create'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:436:in `_run__2695083164076933069__process_action__522201399773442049__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/mapper.rb:42:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:601:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.12) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `_run__1001537658589897327__call__4072859572188315975__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.12) 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.12) 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.12) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.12) lib/rails/engine.rb:479:in `call'
railties (3.2.12) lib/rails/application.rb:223:in `call'
railties (3.2.12) lib/rails/railtie/configurable.rb:30:in `method_missing'

1 个答案:

答案 0 :(得分:3)

问题是config.stretchesconfig/initializers/devise.rb的值太高(不能高于31)。将其更改为较低的值,事情会再次发挥作用。

那我是怎么想到的呢?方法如下:

从堆栈跟踪中,您可以看到在bcrypt-ruby中抛出ArgumentError的函数是:

def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST })
    raise ArgumentError if options[:cost] > 31
    Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost]), options[:cost]))
end

好的,问题是Devise发送的成本高于31(这是允许的最大值)。让我们看看Devise调用(再次来自堆栈跟踪):

def password_digest(password)
    ::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
end

这就是我们如何找到问题的方法。 :)