我一直在嘗試爲忘記密碼的用戶添加密碼重置。用戶點擊忘記密碼?在註冊頁面上。然後,用戶鍵入他們的電子郵件並點擊重置密碼,該密碼會創建一個令牌併發送帶有鏈接的電子郵件以重置其密碼。大多數情況下,它只在電子郵件框爲空白或正好有6個隨機字母/數字時才起作用,但當用戶放入電子郵件並單擊密碼重置時它不起作用,它會顯示錯誤消息:1)PasswordResets在請求密碼重置時重置電子郵件
**Validation failed: Password can't be blank
Password cant be blank, password is too short(6 min)**
通過改變user.rb 驗證:密碼,存在:真,長度:{最小:6} 驗證:password_confirmation,存在:真
我已經得到不同的錯誤,有反正從此重置密碼錶格
中排除此驗證app/models/user.rb:30:in `send_password_reset'
app/controllers/password_resets_controller.rb:7:in `create'
在視頻中出現此錯誤275我如何測試。在11:20
故障/錯誤:click_button「重置密碼」 的ActiveRecord :: RecordInvalid: 驗證失敗:密碼不能爲空,密碼太短(至少是6個字符),密碼確認不能是空白
# ./app/models/user.rb:30:in `send_password_reset'
# ./app/controllers/password_resets_controller.rb:7:in `create'
# (eval):2:in `click_button'
# ./spec/requests/password_resets_spec.rb:9:in `block (2 levels) in <top (required)>'
成品在13.66秒 95實施例中,1次失敗
這是所使用的一些代碼。
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
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
password_resets_controller.rb
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset
redirect_to root_url, :notice => "Email sent with password reset instructions."
end
def edit
@user = User.find_by_password_reset_token!(params[:id])
end
end
password_resets_spec
require 'spec_helper'
describe "PasswordResets" do
it "emails user when requesting password reset" do
user = Factory(:user)
visit signin_path
click_link "password"
fill_in "Email", :with => user.email
click_button "Reset Password"
current_path.should eq(root_path)
page.should have_content("Email sent")
last_email.to.should include(user.email)
end
end
user_spec.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
#
require 'spec_helper'
describe User do
describe "#send_password_reset" do
let(:user) { Factory(:user) }
it "generates a unique password_reset_token each time" do
user.send_password_reset
last_token = user.password_reset_token
user.send_password_reset
user.password_reset_token.should_not eq(last_token)
end
it "saves the time the password reset was sent" do
user.send_password_reset
user.reload.password_reset_sent_at.should be_present
end
it "delivers email to user" do
user.send_password_reset
last_email.to.should include(user.email)
end
end
before do
@user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should respond_to(:admin) }
it { should respond_to(:authenticate) }
it { should be_valid }
it { should_not be_admin }
describe "with admin attribute set to 'true'" do
before { @user.toggle!(:admin) }
it { should be_admin }
end
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[[email protected],com user_at_foo.org [email protected]
[email protected]_baz.com [email protected]+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
@user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[[email protected] [email protected] [email protected] [email protected]]
addresses.each do |valid_address|
@user.email = valid_address
@user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "email address with mixed case" do
let(:mixed_case_email) { "[email protected]" }
it "should be saved as all lower-case" do
@user.email = mixed_case_email
@user.save
@user.reload.email.should == mixed_case_email.downcase
end
end
describe "when password is not present" do
before { @user.password = @user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { @user.password_confirmation = nil }
it { should_not be_valid }
end
it { should respond_to(:authenticate) }
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
it { should == found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
describe "remember token" do
before { @user.save }
its(:remember_token) { should_not be_blank }
end
end
你得到低投票的原因是因爲你的問題描述得非常糟糕。說實話,我不確定你在問什麼(我可以猜到,但那不是重點),爲什麼你給了你所做的代碼。嘗試再次詢問(請更好地)或編輯問題。 –
@theoscholiadis idk爲什麼我收到錯誤信息我收到,我想我的問題是我如何解決它。 –
@TheoScholiadis我注意到,當我從user.rb 長度{{minimum:6})中刪除時,它不再具有該部分錯誤,無論如何,在提交重置密碼時都會拒絕這些驗證? –