简体   繁体   English

UsersController#new中的Rails Gem :: LoadError

[英]Rails Gem::LoadError in UsersController#new

New to Ruby on Rails and having a problem when following Michael Hartl's tutorial.I'm using Rails 3.2.2 with Ruby 1.9.3. Ruby on Rails的新手,在遵循Michael Hartl的教程时遇到了问题。我将Rails 3.2.2与Ruby 1.9.3结合使用。 The issue looks very similar to another question that was raised but was unanswered: Rails Error NoMethodError in UsersController#show error 该问题与提出但未得到解决的另一个问题非常相似: Rails错误NoMethodError in UsersController#show error

I get the following error when attempting to add a new user via /signup 尝试通过/ signup添加新用户时出现以下错误

Gem::LoadError in UsersController#new
bcrypt-ruby is not part of the bundle. Add it to Gemfile.

Reloading the page gives the error: 重新加载页面会出现错误:

NoMethodError in UsersController#new
undefined method `key?' for nil:NilClass

The problem seems to be related to the inclusion of the bcrypt-ruby gem, and the usage of the has_secure_password method in user.rb . 该问题似乎与包含bcrypt-ruby gem和user.rb中has_secure_password方法的使用有关。 Removing the call to has_secure_password in user.rb gets rid of the error and it goes to the signup page successfully. 在user.rb中删除对has_secure_password的调用将消除该错误,并成功转到注册页面。

user.rb: 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: 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

However, I cant find anything wrong with the inclusion of the bcrypt-ruby gem. 但是,我找不到包含bcrypt-ruby gem的任何错误。 In the Gemfile I have: 在Gemfile中,我有:

gem 'bcrypt-ruby', '3.0.1'

and the gem has also been generated in Gemfile.lock : 而且宝石也已经在Gemfile.lock中生成:

DEPENDENCIES
  annotate (~> 2.4.1.beta)
  bcrypt-ruby (= 3.0.1)

I've also added password_digest to the database via migration: 我还通过迁移将password_digest添加到数据库中:

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string

  end
end

Any ideas ? 有任何想法吗 ?

I'm going through the same tutorial and encountered the exact same problem. 我正在阅读同一教程,遇到了完全相同的问题。

My solution was to restart the web server. 我的解决方案是重新启动Web服务器。 After installing the gem, I think the web server needs to be restarted so it is loaded. 安装gem之后,我认为Web服务器需要重新启动才能加载。

Justin 贾斯汀

重新启动网络服务器后,它为我修复了此问题(spork在后台运行以加快测试的运行速度)

Did you tried the 'bundle update' command, usually the bundler will take care of gems if you specified in the Gemfile. 您是否尝试过'bundle update'命令,如果您在Gemfile中指定,通常捆绑程序将处理gems。 If you want to check the gem dependency please check http://rubygems.org/gems . 如果要检查gem依赖项,请检查http://rubygems.org/gems

And if you are using windows(I know its strange- but some of our app works in windows only) there is some tricks to install bcrypt 如果您使用的是Windows(我知道它很奇怪-但我们的某些应用程序仅在Windows中工作),则有些技巧可以安装bcrypt

Steps to install bcrypt. 安装bcrypt的步骤。

1 Download Devkit and extract 1下载Devkit并解压缩

you can download it from here http://rubyinstaller.org/downloads/ 您可以从此处http://rubyinstaller.org/downloads/下载

2 Place devkit it your jruby folder (in my case C:\\applications\\jruby\\devkit) 2将devkit放在您的jruby文件夹中(在我的情况下为C:\\ applications \\ jruby \\ devkit)

3 You need to install ruby as well either 1.8.7 or 1.9(some times needs a system restart) 3您还需要安装ruby 1.8.7或1.9(有时需要重新启动系统)

4 CD into devkit directory 4 CD进入devkit目录

5 Run ruby dk.rb init 5运行ruby dk.rb init

6 Open config.yml and make sure that both your jruby installtion is listed. 6打开config.yml,并确保同时列出了两个jruby安装。 If not, ADD them. 如果没有,请添加它们。 Save and close config.yml after you're done. 完成后,保存并关闭config.yml。

example:- C:/applications/jruby 示例:-C:/ applications / jruby

7 Run ruby dk.rb install 7运行ruby dk.rb安装

8 jruby -S gem install bcrypt-ruby 8 jruby -S gem安装bcrypt-ruby

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM