[英]In Ruby on Rails Restful Authentication, UsersController#new, a @user = User.new is used. Is it actually needed?
[英]Rails Gem::LoadError in UsersController#new
Ruby on Rails的新手,在遵循Michael Hartl的教程時遇到了問題。我將Rails 3.2.2與Ruby 1.9.3結合使用。 該問題與提出但未得到解決的另一個問題非常相似: Rails錯誤NoMethodError in UsersController#show error
嘗試通過/ signup添加新用戶時出現以下錯誤
Gem::LoadError in UsersController#new
bcrypt-ruby is not part of the bundle. Add it to Gemfile.
重新加載頁面會出現錯誤:
NoMethodError in UsersController#new
undefined method `key?' for nil:NilClass
該問題似乎與包含bcrypt-ruby gem和user.rb中has_secure_password方法的使用有關。 在user.rb中刪除對has_secure_password的調用將消除該錯誤,並成功轉到注冊頁面。
user.rb:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
valid_email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: valid_email_regex },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6}
end
users_controller.rb:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome!"
redirect_to @user
else
render 'new'
end
end
end
但是,我找不到包含bcrypt-ruby gem的任何錯誤。 在Gemfile中,我有:
gem 'bcrypt-ruby', '3.0.1'
而且寶石也已經在Gemfile.lock中生成:
DEPENDENCIES
annotate (~> 2.4.1.beta)
bcrypt-ruby (= 3.0.1)
我還通過遷移將password_digest添加到數據庫中:
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
有任何想法嗎 ?
我正在閱讀同一教程,遇到了完全相同的問題。
我的解決方案是重新啟動Web服務器。 安裝gem之后,我認為Web服務器需要重新啟動才能加載。
賈斯汀
重新啟動網絡服務器后,它為我修復了此問題(spork在后台運行以加快測試的運行速度)
您是否嘗試過'bundle update'命令,如果您在Gemfile中指定,通常捆綁程序將處理gems。 如果要檢查gem依賴項,請檢查http://rubygems.org/gems 。
如果您使用的是Windows(我知道它很奇怪-但我們的某些應用程序僅在Windows中工作),則有些技巧可以安裝bcrypt
安裝bcrypt的步驟。
1下載Devkit並解壓縮
您可以從此處http://rubyinstaller.org/downloads/下載
2將devkit放在您的jruby文件夾中(在我的情況下為C:\\ applications \\ jruby \\ devkit)
3您還需要安裝ruby 1.8.7或1.9(有時需要重新啟動系統)
4 CD進入devkit目錄
5運行ruby dk.rb init
6打開config.yml,並確保同時列出了兩個jruby安裝。 如果沒有,請添加它們。 完成后,保存並關閉config.yml。
示例:-C:/ applications / jruby
7運行ruby dk.rb安裝
8 jruby -S gem安裝bcrypt-ruby
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.