1)PasswordResets电子邮件用户请求密码重置时(1) PasswordResets em

2019-09-18 01:08发布

我一直在试图增加一个密码重置为忘记密码的用户。 用户点击忘记密码? 在注册网页。 然后,用户键入他们的电子邮件和点击重置密码,它创建了一个令牌,并发送电子邮件的链接重设密码。 对于它的工作原理,只有当邮箱为空或有且只有6个随机字母/数字,但是当用户将在他的电子邮件并点击密码重置它不工作在大多数情况下,它带来了错误信息:

**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+\-.]+@[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: "user@example.com", 
                 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[user@foo,com user_at_foo.org example.user@foo.
                   foo@bar_baz.com foo@bar+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[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
          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) { "Foo@ExAMPle.CoM" }

        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

Answer 1:

乍一看,重置密码时,你的代码试图挽救一个空密码(因此你的错误)。 尝试使用调试它的一行,时间logger.debug ,试图找到在什么时候,你的代码保存之前勾销的密码。 我没有用过UserMailer,但它看起来像错误是与UserMailer.password_reset(self).deliver线。 如果你写的方法,我想看看调试,第一。 如果是自动的,尝试和调试将所有参数设置为邮件生成令牌和重置密码的部分。

希望这可以帮助。



Answer 2:

在你send_password_reset方法你使用:

save!

改用

save!(validate: false)

这样就可以了。 问题是,您试图保存模型和验证的干扰。 你不需要任何验证的send_password_reset方法,因为有来自用户生成什么都没有,所以没有被保存到数据库中的无效信息的危险。



文章来源: 1) PasswordResets emails user when requesting password reset