简体   繁体   English

Devise :: SessionsController#create中的NameError

[英]NameError in Devise::SessionsController#create

I'm trying to use :lockable , and so when I add it to class User < ActiveRecord::Base and try to sign in or log in . 我正在尝试使用:lockable ,所以当我将其添加到class User < ActiveRecord::Base并尝试登录或登录时。 I get redirect to and error page. 我得到重定向到错误页面。 Here's what I get 这就是我得到的

NameError in Devise::SessionsController#create
undefined local variable or method `locked_at' for #<User:0x000001025a56d8>

Rails.root: /Users/user/Ruby

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"dsfgsgfddsfgt5467657654n74=",
 "user"=>{"email"=>"email@email.com",
 "password"=>"[FILTERED]"},
 "commit"=>"Sign in"}
Toggle session dump
Toggle env dump
Response

Headers:

None

Any ideas? 有任何想法吗? When I remove :lockable it all works fine 当我删除:lockable ,一切正常

Devise requires datetime :locked_at field. 设计需要datetime :locked_at字段。 You must missed it. 你一定错过了。

UPDATE: 更新:

Include this migration as it looks like you dont have appropriate field in your user table. 包括此迁移,因为看起来您的user表中没有适当的字段。

do this 做这个

  1. rails g migration add_devise_required_missing_fields

  2. Open the generated migration file and paste this 打开生成的迁移文件并将其粘贴

     change_table(:users) do |t| ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts t.string :unlock_token # Only if unlock strategy is :email or :both t.datetime :locked_at end add_index :users, :unlock_token, unique: true 
  3. lastly , run rake db:migrate 最后,运行rake db:migrate

There is two possible mistakes, 有两个可能的错误,

  1. You may missed to enable the fields which need devise lockable feature 您可能会错过启用需要设计可锁定功能的字段的机会

    ## Lockable ##可锁

    # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts #t.integer:failed_attempts,默认值:0,null:false#仅当锁定策略为:failed_attempts时

    # t.string :unlock_token # Only if unlock strategy is :email or :both #t.string:unlock_token#仅当解锁策略为:email或:both时

    # t.datetime :locked_at #t.datetime:locked_at

You have to enable and migrate, in other word, these three inputs needed in User(if you applied devise to User model) table. 换句话说,您必须启用并迁移User(如果您对用户模型应用了devise)表中所需的这三个输入。

  1. You may used rake db:setup command to create, migrate and seed Database. 您可以使用rake db:setup命令来创建,迁移和播种数据库。 This is correct but you have to check, whether you have those three fields in db/schema.rb file because db:setup command will check schema file and run the migration. 这是正确的,但是您必须检查db / schema.rb文件中是否具有这三个字段,因为db:setup命令将检查架构文件并运行迁移。 May be other co-developer who working with the same project add that field and forget to upload schema.rb changes to git/bitbucket. 可能是其他与同一项目一起工作的共同开发人员添加了该字段而忘记将schema.rb更改上载到git / bitbucket。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM