user3537990
user3537990

Reputation:

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 . 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

Upvotes: 2

Views: 2045

Answers (2)

SaraVanaN
SaraVanaN

Reputation: 329

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.string :unlock_token # Only if unlock strategy is :email or :both

    # 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.

  1. You may used rake db:setup command to create, migrate and seed Database. 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. May be other co-developer who working with the same project add that field and forget to upload schema.rb changes to git/bitbucket.

Upvotes: 0

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8132

Devise requires datetime :locked_at field. You must missed it.

UPDATE:

Include this migration as it looks like you dont have appropriate field in your user table.

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

Upvotes: 1

Related Questions