1

Loosely following Ryan Bates's How I Test Railscast to implement sending an email confirmation token to users when they sign up.

class User < ActiveRecord::Base
  has_secure_password
  strip_attributes except: [:password, :password_confirmation]
  ...

  def send_email_confirmation
    generate_token(:email_token)
    self.email_token_sent_at = Time.zone.now
    save!
    UserMailer.email_confirmation(self).deliver
  end

  private
    def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end while User.exists?(column => self[column])
    end
end

This is failing (in feature specs and when manually clicking through sign-up process) with:

Failures:

  1) UserPages sign up with valid information should create a user
     Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
     BCrypt::Errors::InvalidSalt:
       invalid salt
     # ./app/models/user.rb:70:in `send_email_confirmation'
     # ./app/controllers/users_controller.rb:27:in `create'
     # ./spec/features/user_pages_spec.rb:165:in `block (5 levels) in <top (required)>'
     # ./spec/features/user_pages_spec.rb:165:in `block (4 levels) in <top (required)>'

I've tried reinstalling bcrypt gem (as suggested elsewhere even though it was Devise related and I'm not using Devise): gem uninstall bcrypt-ruby and then gem install bcrypt-ruby but to no avail. ideas?

4

2 回答 2

0

Gemstrip_attributes应对此类行为负责。

解决方案

从剥离中排除:password_digest属性,如下所示:

strip_attributes except: [:password_digest]
于 2016-06-29T07:40:34.643 回答
0

我们遇到了这个错误,这是因为我们试图在一个只有数据库读取权限的 k8s pod 中执行写入操作。

我确定这不是原始问题的答案,但是当我们试图找出导致我们这个错误的原因时,我遇到了这个问题,所以我想分享一下。

于 2021-05-27T18:16:33.277 回答