Search code examples
herokuruby-on-rails-4nomethoderror

Rails 4.0 on Heroku, NoMethodError (undefined method `password_digest=' for #<User


I've seen lots of posts about this error, but I'm still stumped.

Edit: https://anthonyroberts.herokuapp.com/signup

Michael Hartl's Tutorial, end of Chapter 7, you have a working signup form. Works locally, but returns 500 on Heroku. I've reset the db and run db:migrate on Heroku, to no avail. I tried the Heroku console, and successfully created a User with

foo = User.create(name: "Anthony", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar")

This works, and I've got a user. So why does the signup form submission give me this error:

NoMethodError (undefined method `password_digest=' for #<User:0x007f02972b7118>): 
    app/controllers/users_controller.rb:12:in `create' 

here's the user.rb:

class User < ActiveRecord::Base
    before_save { email.downcase! }
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, length: { minimum: 6 }
end

here's the migration to add the password_digest to the model:

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

Here's the users_controller.rb:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end
end

Here's the new.html.erb template:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

I'm new to RoR, so I'll bet it's a piddly mistake.

Thanks in advance.


Solution

  • I just had the same problem and fixed it by running the command heroku restart.