【问题标题】:NameError in UsersController - (uninitialized constant SCrypt)UsersController 中的 NameError -(未初始化的常量 SCrypt)
【发布时间】:2014-03-04 11:06:13
【问题描述】:

在我使用 Rails 4.1.0Ruby 2.1.0 的 Rails 应用程序中,

我一直在使用 Authlogic 对用户进行身份验证。

users_controller.rb 中,我有一个如下所示的创建方法。

def create
  @user = User.new(user_params) #this line has the error
  respond_to do |format|
    if @user.save
      format.html { redirect_to_target_or_default account_url, notice: 'User was successfully created.' }
    else
      format.html { render action: 'new' }
    end
  end
end

由于在 Rails 4.0 中强烈推荐使用强参数,attr_accessible 已从 User 模型中删除,并将以下给定代码添加到 users_controller.rb

private

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:login, :email, :password, :password_confirmation, :role_ids)
    end

用户.rb

class User < ActiveRecord::Base

  #attr_accessible :login, :email, :password, :password_confirmation, :role_ids

  has_many :articles
  has_many :comments
  has_many :assignments

  has_many :roles, :through => :assignments

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end


  acts_as_authentic do |c|
    c.login_field = :login
  end


  def deliver_password_reset_instructions!
    reset_perishable_token!
    Notifier.deliver_password_reset_instructions(self)
  end

end

现在,当我尝试使用下面给出的表格进行注册时,

我收到以下附加错误。请帮我解决。

【问题讨论】:

  • SCrypt 是一个 Ruby gem,我想你用它来做密码散列?您可能没有安装 gem,或者它没有添加到您的 Gemfile 中。 rubygems.org/gems/scrypt
  • 你能发布你的用户模型吗? authlogic 使用 SCrypt 来解密密码。这被调用了,但没有找到。我会检查你的 authlogic 实现是否正确。你确定 authlogic gem 支持 rails 4.1 和 ruby​​ 2.1.1
  • @Andreas Lyngstad:问题已更新。我希望 Authlogic 支持 Rails 4.0
  • @Rajesh CO 也许您应该接受我的回答以供将来参考?
  • @AndreasLyngstad:接受

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 strong-parameters


【解决方案1】:

来自 authlogic github 帐户上的 issue

Authlogic 已将其默认加密系统从 SHA512 更改为 SCrypt。

您的 gemfile 中似乎需要这个

gem 'authlogic', '~> 3.4.0'
gem 'scrypt'

如果你不想要 SCrypt,你可以通过放置这个来使用 Sha512

acts_as_authentic do |c|
  c.crypto_provider = Authlogic::CryptoProviders::Sha512
end

在你的 User.rb 中

您可能还需要指定 authlogic gem 的版本

gem 'authlogic', github: 'binarylogic/authlogic', ref: 'e4b2990d6282f3f7b50249b4f639631aef68b939'

但我想这很快就会解决

【讨论】:

  • 你的 gemfile 里有什么
【解决方案2】:

问题出在 Authlogic gem 上。为了让它被 Rails 4.0 支持,我们需要从下面给出的 github URL 获取它,

gem 'authlogic', github: 'binarylogic/authlogic', ref: 'e4b2990d6282f3f7b50249b4f639631aef68b939'

然后强参数的实现也可以正常工作..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多