Gargob
Gargob

Reputation: 251

Rails not saving model field

I was wondering why my rails is not saving my encrypted_password field.

Here is my UserController

class UserController < ApplicationController
  require 'bcrypt'
  before_filter :save_login_state, :only => [:new, :create]
  def new
    new_user = User.new(user_params)
    new_user.numRatings = 0    
    if new_user.save
      flash[:notice] = "You signed up successfully"
      flash[:color]= "valid"
    else
      flash[:notice] = "Form is invalid"
      flash[:color]= "invalid"
    end
    redirect_to(:controller => 'sessions', :action => 'login')
  end

  def create
  end

  def update
  end

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

Here is my User model

class User < ActiveRecord::Base
    require 'BCrypt'
    attr_accessor :password, :encrypted_password
    EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
    validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
    validates :name, :presence => true
    validates :password, :confirmation => true, :presence => true, :on => :create

    before_save :encrypt_password
    after_save :clear_password

    def encrypt_password
      if password.present?
        self.salt = BCrypt::Engine.generate_salt
        self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
      end
    end

    def clear_password
      self.password = nil
    end

    def self.authenticate(username_or_email="", login_password="")
      if  EMAIL_REGEX.match(username_or_email)    
        user = User.find_by_email(username_or_email)
      end
      if user && user.match_password(login_password)
        return user
      else
        return false
      end
    end   

    def match_password(login_password="")
      encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
    end

end

In addition, I use this function to save it

  def login_attempt
    authorized_user = User.authenticate(params["user"]["email"],params["user"]["password"])
    if authorized_user
        session[:user_id] = authorized_user.id
        flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
        redirect_to(:action => 'home')
    else
        flash[:notice] = "Invalid Username or Password"
        flash[:color]= "invalid"
        render "login"  
    end
  end

One thing that I suspect that I created my first migration

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password
      t.string :name
      t.float :rating
      t.integer :numRatings

      t.timestamps
    end
  end
end

However, I changed the :password field to :encrypted_password and it's reflected in a the table. I've been stuck on this for 2 hours. I was wondering if anything sticks out. Thanks.

The logs say that the data is being injected minus the encrypted password

INSERT INTO "users" ("created_at", "email", "name", "numRatings", "salt", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", "2014-08-05 06:22:41.335373"], ["email", "[email protected]"], ["name", "w"], ["numRatings", 0], ["salt", "$2a$10$pagmnNzT4FWEBmaPmiLX1u"], ["updated_at", "2014-08-05 06:22:41.335373"]]

Upvotes: 1

Views: 2075

Answers (2)

Vasseurth
Vasseurth

Reputation: 6476

You mentioned you changed the migration :password field to :encrypted_password field, but are you sure you reinitialized the models? I.e. correctly migrated the changes? Finally, if you did change the migration, there is no need for an attr_accessor of encrypted_password. Having both of them may cause shadowing and be the problem.

Upvotes: 0

sevenseacat
sevenseacat

Reputation: 25029

Your attr_accessor :encrypted_password sticks out - this is overwriting the Rails-generated attribute getter/setter with one that will simply set an instance variable called @encrypted_password in your model instance.

Upvotes: 5

Related Questions