gpt4 book ai didi

ruby - ActiveModel::SecurePassword:Module 的未定义方法 `min_cost'

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:55 24 4
gpt4 key购买 nike

我正在关注 Michale harlt 的 ruby​​ on rails 教程。一切都很顺利,直到我陷入这个问题,在输入用户名和密码并单击“登录”按钮后,我收到了一个错误 ” ActiveModel::SecurePassword:Module 的未定义方法“min_cost”。我试图解决过去 2 天的这个问题,但找不到任何相关的解决方案。请帮我解决这个问题。提前致谢。这里是我项目的所有相关文件。 (我正在使用 rails 3.2.16)如需更多信息,请告诉我。

用户.rb

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

# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end

# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end

# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end

# Returns true if the given token matches the digest.
def authenticated?(remember_token)
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
end

sessions_helper.rb

module SessionsHelper

# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end

# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end

# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by_id(user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by_id(user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end

# Log Out Current User
def log_out
session.delete(:user_id)
@current_user = nil
end
end

session Controller .rb

    class SessionsController < ApplicationController

def new
end

def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
remember user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
log_out
redirect_to home_path
end
end

gem 文件

    source 'https://rubygems.org'

gem 'rails', '3.2.16'
gem 'bcrypt-ruby', '~> 3.1.2'
gem 'strong_parameters'



# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'guard'


end

group :test do
gem 'minitest-reporters'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end

group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '2.11.1'

end


# Gems used only for assets and not required
# in production environments by default.
group :assets do

gem 'coffee-rails', '~> 3.2.1'
gem 'sass-rails', '>= 3.2'
gem 'bootstrap-sass', '~> 3.3.4'
gem 'sprockets-rails', '=2.0.0.backport1'
gem 'sprockets', github: 'tessi/sprockets', branch: '2_2_2_backport2'


# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

应用程序跟踪

    app/models/user.rb:14:in `digest'
app/models/user.rb:27:in `remember'
app/helpers/sessions_helper.rb:10:in `remember'
app/controllers/sessions_controller.rb:10:in `create'

最佳答案

据我所知,SecurePassword#min_cost出现在 rails4(或 rails3 > 3.2.16.)

由于您只是在学习,我的建议是不要专注于解决这个特定错误,而只是将有问题的行替换为:

cost = BCrypt::Engine::MIN_COST

甚至用

cost = 10

然后走得更远。无论您是否仍要解决此问题,您都应该将导轨升级到 4(或引入此方法的任何版本。)

关于ruby - ActiveModel::SecurePassword:Module 的未定义方法 `min_cost',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com