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

查看:110
本文介绍了Rails的Gem :: LoadError UsersController#新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby on Rails新手,在遵循Michael Hartl的教程时遇到了问题。我在Ruby 1.9.3中使用Rails 3.2.2。这个问题与提出的另一个问题看起来非常相似,但未得到答复:
Rails在UsersController中显示错误NoMethodError#显示错误

尝试通过/注册添加新用户时出现以下错误

  UsersController中的Gem :: LoadError#new 
bcrypt-ruby不是捆绑包的一部分。将它添加到Gemfile。

重新加载页面会产生错误:

 UsersController中的NoMethodError#new 
未定义的方法`key?'for nil:NilClass

这个问题似乎与包含bcrypt-ruby gem以及user.rb中has_secure_password方法的使用有关。删除user.rb中对has_secure_password的调用可以消除错误,并成功进入注册页面。



user.rb:

 #==模式信息

#表名:users

#id:integer not null ,主键
#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+\-.]+@[az\d\-.]+\.[az]+\z/i
验证:电子邮件,存在:true,
格式:{with:valid_email_regex},
唯一性:{case_sensitive:false}
验证:密码,长度:{minimum:6}
结束

users_controller.rb:

  class UsersController< ApplicationController 
def new
@user = User.new
end
def create
@user = User.new(params [:user])
if @ user.save
flash [:success] =欢迎!
redirect_to @user
else
渲染'新'
结束
结束
结束

然而,我无法找到包含bcrypt-ruby gem的任何错误。在Gemfile中我有:

  gem'bcrypt-ruby','3.0.1'


,并且Gem也已经在Gemfile.lock中生成:

 依赖项
注释(〜> 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

有什么想法吗?

解决方案

更新'命令,如果你在Gemfile中指定,通常捆绑器会照顾宝石。如果您想检查gem依赖项,请查看 http://rubygems.org/gems



如果您使用的是Windows(我知道它很奇怪,但是我们的一些应用程序只能在windows中运行),那么还有一些技巧可以安装bcrypt

安装bcrypt的步骤。

1下载Devkit并解压缩



您可以从这里下载 http://rubyinstaller.org/downloads/



2将devkit放在您的jruby文件夹中(在我的情况下为C:\applications\jruby\devkit)

<3>您需要安装红宝石以及1.8。 7或1.9(有时需要系统重新启动)

4 CD放入devkit目录

<5>运行ruby dk .rb init



6打开config.yml并确保列出了您的jruby installtion。如果没有,请添加它们。例如: - C:/ applications / jruby



> 7运行ruby dk.rb install
$ b $ 8 jruby -S gem install bcrypt-ruby

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 ?

解决方案

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

这篇关于Rails的Gem :: LoadError UsersController#新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆