0

I keep getting this error when I try to run my application. It states that there is an undefined method error in the application, but doesn't state where. The closest error I could find as tangible was this:

NameError (undefined local variable or method `confirmed_at' for #<User:0x6049800>):

I'm not sure which part this is directing to. Could someone please tell me what this error means?

This is the code for the devise_users file

class DeviseCreateUsers < ActiveRecord::Migration
def change
   create_table(:users) do |t|
    ## Database authenticatable
    t.string :name,               null: false, default: ""
    t.string :email,              null: false, default: ""
    t.string :encrypted_password, null: false, default: ""
    t.string :about
    t.string :avatar
    t.string :cover

    ## Recoverable
    t.string   :reset_password_token
    t.datetime :reset_password_sent_at

    ## Rememberable
    t.datetime :remember_created_at

    ## Trackable
    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
    t.string   :confirmation_token
    t.datetime :confirmed_at
    t.datetime :confirmation_sent_at
    t.timestamps null: false
    ## 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


    t.timestamps null: false
  end

  add_index :users, :email,                unique: true
  add_index :users, :reset_password_token, unique: true
  add_index :users, :confirmation_token, unique: true
  # add_index :users, :confirmation_token,   unique: true
  add_index :users, :unlock_token,         unique: true
 end
 end

There are not any other places within the application where the method is called, therefore the error states that the method is being called somewhere when it isn't. How can I fix this error?

metaco57
  • 155
  • 3
  • 15

2 Answers2

1

Your migration file DeviseCreateUsers created a table users in the database, and one of the columns in that table is confirmed_at.

In your app/models/user.rb file you probably declared your devise configuration like:

class User < ActiveRecord::Base
  devise :registerable, :confirmable

  ...
end

ActiveRecord::Base, the class User is inheriting from, automatically creates "getter" and "setter" methods for database columns (i.e. confirmed_at), so the method #confirmed_at should already be defined.

So if the method is "missing", you may not have run the database migrations (e.g. rake db:migrate)

Dillon Hafer
  • 156
  • 1
  • 8
  • Similarly, you could try to [regenerate the migration file](http://stackoverflow.com/a/9108621/3345375) and/or [re-run the migration](http://stackoverflow.com/a/13475181/3345375). – jkdev Feb 18 '16 at 00:38
  • The program won't let me migrate the file. It states that there is a not a method defined called confirmable in the regenerated migration file. Also, how would I use t.confirmable properly in the application? – metaco57 Feb 18 '16 at 04:25
  • It is correct about the undefined `t.confirmable`. `t.confirmable` would not be used in the migration file itself, `devise :confirmable` would go in your user model. Your migration file simply needs the following: `t.string :confirmation_token`, `t.datetime :confirmed_at`, and `t.datetime :confirmation_sent_at` – Dillon Hafer Feb 18 '16 at 04:30
  • This is exactly what I have. Only this time, it states that there is an ArgumentError in Devise::Registrations#Create – metaco57 Feb 18 '16 at 04:42
  • Are you still getting the `NameError (undefined local variable or method 'confirmed_at' for #):` error? – Dillon Hafer Feb 18 '16 at 04:43
  • No, that's fixed. Right now this is a different error. – metaco57 Feb 18 '16 at 04:47
0

Search your code for confirmed_at -- it seems you used it somewhere without defining it first.

It would be nice if the error message gave you a file name and a line number, but (depending on which text editor or IDE you're using) you might be able to run a search on the entire Rails app at once.

My guess is you called the confirmed_at method on a user object (user_1.confirmed_at, for example) and the method wasn't defined in class User.

jkdev
  • 11,360
  • 15
  • 54
  • 77
  • I have found the confirmed_at method, but it is only mentioned at the part where the method is declared. Also, there are no other uses of 'confirmed_at' in the application. How would this create an error? – metaco57 Feb 17 '16 at 16:54
  • I'm not sure. Maybe there's an issue with the syntax and it messes up the method definition? (Like an extra or missing `end` keyword.) – jkdev Feb 17 '16 at 23:03
  • Please post your code (in context) and let's take a look at it. – jkdev Feb 17 '16 at 23:04