Rails Gem::LoadError in UsersController#new

2019-08-25 00:54发布

问题:

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. The issue looks very similar to another question that was raised but was unanswered: Rails Error NoMethodError in UsersController#show error

I get the following error when attempting to add a new user via /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 . 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:

# == 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

However, I cant find anything wrong with the inclusion of the bcrypt-ruby gem. In the Gemfile I have:

gem 'bcrypt-ruby', '3.0.1'

and the gem has also been generated in Gemfile.lock :

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

I've also added password_digest to the database via migration:

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

  end
end

Any ideas ?

回答1:

Did you tried the 'bundle update' command, usually the bundler will take care of gems if you specified in the Gemfile. If you want to check the gem dependency please check 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

Steps to install bcrypt.

1 Download Devkit and extract

you can download it from here http://rubyinstaller.org/downloads/

2 Place devkit it your jruby folder (in my case 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)

4 CD into devkit directory

5 Run ruby dk.rb init

6 Open config.yml and make sure that both your jruby installtion is listed. If not, ADD them. Save and close config.yml after you're done.

example:- C:/applications/jruby

7 Run ruby dk.rb install

8 jruby -S gem install bcrypt-ruby



回答2:

I'm going through the same tutorial and encountered the exact same problem.

My solution was to restart the web server. After installing the gem, I think the web server needs to be restarted so it is loaded.

Justin



回答3:

Restarting the web server fixed it for me (had spork running in the background to speed up the running of the tests)