我尝试查看有关此错误的先前问题和答案,但解决方案似乎对我不起作用。在线抛出错误:
if customer && customer.authenticate(params[:session][:password_digest])
我能够注册一个用户(在一家公司下)并注册一家公司,但每当我尝试登录用户时,我都会收到此 BCrypt 错误。一旦我的用户模型和公司模型等,我才开始使用 has_secure_password 等内容. 到位,我后来才意识到不保护密码是愚蠢的。即使我现在创建一个新用户,登录时仍然会出现此错误。
我认为这可能是由于我与用户的关系?(也许完全不是,我对 Ruby 很陌生!)我的用户属于一家公司(多对一),当他们注册时,他们从列表中选择一家公司,以便在数据库中列出。这是我与此错误相关的代码:
class SessionsController < ApplicationController
def new
end
def create
customer = Customer.find_by_email(params[:session][:email])
if customer && customer.authenticate(params[:session][:password_digest])
log_in customer #see sessions helper
remember customer #see sessions helper
redirect_to '/main'
else
redirect_to '/login'
end
end
def destroy
#session[:user_id] = nil
forget(current_customer)
session.delete(:customer_id)
@current_customer = nil
redirect_to '/'
end
end
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.timestamps
t.references :business, foreign_key: true
t.timestamps
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest
t.string :remember_digest
t.string :role
end
end
end
class CustomersController < ApplicationController
def new
@customer = Customer.new
@businesses = Business.all
end
def create
@customer = Customer.create(customer_params)
@customer.save!
session[:customer_id] = @customer.id
redirect_to '/'
rescue ActiveRecord::RecordInvalid => ex
render action: 'new', alert: ex.message
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password_digest, :business_id) #replace company with company ID
end
end
请帮帮我,我把头发扯掉了:(