簡體   English   中英

Devise :: SessionsController#create中的NameError

[英]NameError in Devise::SessionsController#create

我正在嘗試使用: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 ,一切正常

設計需要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

有兩個可能的錯誤,

  1. 您可能會錯過啟用需要設計可鎖定功能的字段的機會

    ##可鎖

    #t.integer:failed_attempts,默認值:0,null:false#僅當鎖定策略為:failed_attempts時

    #t.string:unlock_token#僅當解鎖策略為:email或:both時

    #t.datetime:locked_at

換句話說,您必須啟用並遷移User(如果您對用戶模型應用了devise)表中所需的這三個輸入。

  1. 您可以使用rake db:setup命令來創建,遷移和播種數據庫。 這是正確的,但是您必須檢查db / schema.rb文件中是否具有這三個字段,因為db:setup命令將檢查架構文件並運行遷移。 可能是其他與同一項目一起工作的共同開發人員添加了該字段而忘記將schema.rb更改上載到git / bitbucket。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM