同时使用Devise和has_secure_password时的ArgumentError

时间:2016-08-09 00:20:50

标签: ruby-on-rails ruby devise

在添加Devise gem之前我有一个工作模型。安装Devise并运行rails generate devise User后,我开始出现此错误。

每当我去rails console并键入

User.first

User.create(username: "Iggy1", email: "iggy1@gmail.com", password: "helloworld", password_confirmation: "helloworld")

它总是返回

  User Load (0.3ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
ArgumentError: wrong number of arguments (0 for 1)
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/devise-4.2.0/lib/devise/models/database_authenticatable.rb:146:in `password_digest'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/activemodel-4.2.5/lib/active_model/serialization.rb:108:in `block in serializable_hash'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/activemodel-4.2.5/lib/active_model/serialization.rb:108:in `each'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/activemodel-4.2.5/lib/active_model/serialization.rb:108:in `serializable_hash'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/serialization.rb:17:in `serializable_hash'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/devise-4.2.0/lib/devise/models/authenticatable.rb:114:in `serializable_hash'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/devise-4.2.0/lib/devise/models/authenticatable.rb:120:in `inspect'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/iggy/.rvm/gems/ruby-2.2.2/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我不断收到wrong number of arguments (0 for 1)错误。我可以理解它为create方法返回参数错误(也许我指定了较少的参数),但我不明白为什么它在User.first上显示错误。

这就是我所拥有的:

users_controller.rb

  ...
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
  end

  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end


Schema.rb

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "password_digest"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end


user.rb (model)

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :lists
  has_many :items, through: :lists
  has_secure_password

数据库中至少有一个用户。在localhost索引上,它显示(我正在使用序列化程序):

{"users":[{"id":1,"name":"Iggy1","email":"letsmailiggy@gmail.com","items":[],"lists":[]}]}

我在模型上也有has_secure_password。这可能是原因吗?我不确定has_secure_password是否与Devise兼容。如何修复它以便我可以在rails控制台上创建用户?

如果您需要更多代码段,请告诉我们!

1 个答案:

答案 0 :(得分:5)

如果查看docs,您可以看到has_secure_password的作用:

  

添加用于设置和验证BCrypt密码的方法。此机制要求您具有password_digest属性。

您没有password_digest属性,因此错误。

但真正的问题是,如果你正在使用Devise,那么根本不需要has_secure_password。如果您想要自己进行身份验证,通常会使用它,Devise已经负责加密和存储密码。从模型中删除它,一切都应该有效。