【问题标题】:NameError in Devise::SessionsController#create设计中的 NameError::SessionsController#create
【发布时间】:2014-05-24 21:13:14
【问题描述】:

我正在尝试使用:lockable,因此当我将其添加到class User < ActiveRecord::Base 并尝试登录或登录时。我重定向到错误页面。这是我得到的

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

有什么想法吗?当我删除 :lockable 时,一切正常

【问题讨论】:

  • 已经试过那个帖子@IsmaelAbreu
  • 但这应该是您错误的解决方案。也许您忘记运行迁移或重新启动服务器。我不能做任何其他可能导致此错误的事情
  • 刚刚尝试迁移并重新启动它。仍然是同样的错误@IsmaelAbreu
  • 好的,然后添加有关您的用户模块和架构的更多信息

标签: ruby-on-rails ruby validation devise


【解决方案1】:

设计需要datetime :locked_at 字段。你一定错过了。

更新:

包含此迁移,因为您的 user 表中似乎没有适当的字段。

这样做

  1. rails g migration add_devise_required_missing_fields

  2. 打开生成的迁移文件并粘贴这个

    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. 最后,运行rake db:migrate

【讨论】:

  • 打开控制台给我User.new.attributes输出。
  • 是的,用户模型没有locked_at 字段。您可能有待处理的迁移,或者您可能会在迁移后更改设计生成的文件,以便不会发生更改。您可以通过检查数据库中的user 表来交叉检查。
  • 我已经运行db:migrate 并重新启动了服务器
  • 待定意味着多长时间?
  • 我在检查user 时仍然看不到它...我要编辑_devise_create_users.rb 吗?
【解决方案2】:

有两个可能的错误,

  1. 您可能错过了启用需要设计可锁定功能的字段

    ## 可锁定

    # t.integer :failed_attempts, default: 0, null: false # 仅当锁定策略为:failed_attempts

    # t.string :unlock_token # 仅当解锁策略为 :email 或 :both 时

    # t.datetime :locked_at

您必须启用和迁移,换句话说,用户表中需要的这三个输入(如果您将设计应用于用户模型)。

  1. 您可以使用 rake db:setup 命令来创建、迁移和播种数据库。这是正确的,但您必须检查 db/schema.rb 文件中是否有这三个字段,因为 db:setup 命令将检查架构文件并运行迁移。可能是其他合作开发者在同一项目中添加了该字段并忘记将 schema.rb 更改上传到 git/bitbucket。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多