0

I am trying to make simple authentication/registration ruby app. I am using BCrypt gem for crypting and it now gives me some problems. When I press submit button it throws

undefined method `password_salt=' for #<User:0x007f93c36bc570>

Ok, so I red that this code should be places from model to coontroler, but that gave me this er

undefined local variable or method `encrypt_password' for #<User:0x007f93c5f35f10>

I have tried rake db:migrate and restarting app also git repo: https://github.com/pumpurs/auth

(code im talking about)

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

controller:

class UsersController < ApplicationController
  def new
  @user = User.new
end
def create
@user = User.new(params[:user].permit(:password, :email, :password_confirmation))
  if @user.save
  redirect_to root_url, :notice => "Signed up!"
else
  render "new"
end
  end

  private

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

model file:

class User < ActiveRecord::Base
  attr_accessor :password, :salt
  before_save :encrypt_password
  validates_confirmation_of :password
  validates_presence_of :password, :on => :create 
  validates_presence_of :email
  validates_uniqueness_of :email
   def encrypt_password
if password.present?
  self.password_salt = BCrypt::Engine.generate_salt
  self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
  end
end
andris
  • 99
  • 4
  • 13

1 Answers1

1

Add

 attr_accessor :password_salt

in model?

Why don't use Devise or another authentication gem?

just so
  • 1,088
  • 2
  • 11
  • 23