NoMethodError: undefined method `password_digest=

I kept coming across this error when I ran the rails console and tried to set a user password:

NoMethodError: undefined method `password_digest=

It wasn’t written in the guide, but to solve this problem, you have to run a migration and create a new database which I called AddPasswordDigestToUsers (following Hartl’s code on Github).

$ rails generate migration AddPasswordDigestToUsers password_digest: string

Then a new file will be created with the following code written in it:

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string

Michael Hartl’s code on Github

Now, you should be able to run the $rails console and add a password to your user.

$rails console
>> User.first
>>user = User.first
>>user.password = “fresh”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s