Ruby on rails 使用Rails 4进行密码确认

Ruby on rails 使用Rails 4进行密码确认,ruby-on-rails,password-confirmation,Ruby On Rails,Password Confirmation,我在一个项目上工作,我需要做一个密码重置系统。 我没有用宝石 邮件/密码_reset.rb class PasswordReset < ActionMailer::Base default from: 'no-reply@educx.com' def send_password_reset(user) @user = user @reset_link = edit_password_resets_url({ token: @user.password_

我在一个项目上工作,我需要做一个密码重置系统。 我没有用宝石

邮件/密码_reset.rb

class PasswordReset < ActionMailer::Base

default from: 'no-reply@educx.com'

def send_password_reset(user)
    @user = user

    @reset_link = edit_password_resets_url({
        token: @user.password_reset_token
    })

    mail({
        :to => user.email, 
        :bcc => ['reset password <resetpassword@educx.com'],
        :subject => I18n.t('password_reset.send_password_reset.subject')
    }) 
end

end

发送的电子邮件没有错误。但当我在
/Password\u resets/Edit?token=fewgfeggrf
点击“编辑密码”时,我被重定向到用户的perfil!我如何更改它?

检查日志,您会发现请求在哪里暂停,重定向在哪里触发,当我点击编辑密码按钮时,当我看到日志时,它会告诉你你需要检查的前过滤器的确切位置。服务器日志应该显示请求触发重定向以及执行停止的位置,只需执行请求并检查服务器正在运行的控制台,在请求的末尾必须有一行类似“执行暂停在:you_before_filter”的内容。好吧,我在2015-02-02 22:18:21-0200 UsersController处理127.0.0.1时得到了`启动补丁程序'/pt/users/1'',更新为HTML参数:{“utf8=>”✓", "真实性令牌“=>”AhQSH6mAiDdlqophAyDmfrQeN/9DVhuzV4xvh5i24FA=“,”用户“=>{”密码“=>”[FILTERED],”密码确认“=>”[FILTERED],”提交“=>”Att user“,”区域设置“=>”pt“,”id“=>”1“}重定向到过滤器链暂停为:可以更改呈现的或重定向的“,”检查可以更改的过滤器,如果您已修复该过滤器,也许你需要在can_change filter应用之前让用户使用令牌,或者可能只是在can_filter之前添加skip_:can_change如果你不需要它
<h2><%= t '.greetings', full_name: @user.full_name %></h2>
 <p><%= t '.body_html', link: link_to(t('.click_here'), @reset_link) %></p>
class PasswordResetsController < ApplicationController
before_action :require_no_authentication, only: [:new, :create, :edit, :update]

def new

end

def create
    user = User.find_by(email: params[:email])

    if user.present?

        user.generate_password_reset

        PasswordReset.send_password_reset(user).deliver

        redirect_to root_url, notice: t('flash.notice.check_email_reset')
    else 
        flash[:alert] = t('flash.alert.cannot_find_email_reset')
        render :new
    end
end

def edit
    @user = User.find_by(password_reset_token: params[:token])
end

def update
    @user = User.find_by!(password_reset_token: params[:token])

    if @user.password_reset_sent_at < 2.hours.ago
        redirect_to new_password_reset_path, alert: t('flash.alert.time_expired')
    end

    if @user.update(password_reset_user_params)
        @user.password_reseted!
        redirect_to new_user_sessions_path, notice: t('flash.notice.password_reseted_complete')

    else
        render :edit
    end
end

private

def password_reset_user_params
    params.require(:user).permit(:password, :password_confirmation)
end
class User < ActiveRecord::Base

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_BIRTHDAY_REGEX = /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/

validates_presence_of :full_name, :email, :birthday, :about

validates_length_of :about, minimum: 10, maximum: 100

validates_format_of :email, with: VALID_EMAIL_REGEX
validates_uniqueness_of :email
validates_format_of :birthday, with: VALID_BIRTHDAY_REGEX 

has_secure_password

scope :confirmed, -> { where.not(created_at: nil) }

before_create do |user|
    user.confirmation_token = SecureRandom.urlsafe_base64
end

def confirm!
    return if confirmed?

    self.confirmed_at = Time.current
    self.confirmation_token = ''

    save!
end 

def confirmed?
    confirmed_at.present?
end

def self.authenticate(email, password)
    user = confirmed.find_by(email: email)

    if user.present?
        user.authenticate(password)
    end
end

def generate_password_reset
    self.password_reset_token = SecureRandom.urlsafe_base64
    self.password_reset_sent_at = Time.zone.now
    save!
end

def password_reseted?
    password_reset_token.present?
end

def password_reseted!
    return if password_reseted?

    self.password_reset_token = ''
    self.password_reseted_at = Time.current

    save!
end


def password_reseted_expired?
    password_reset_sent_at < 1.hours.ago
end
<%= form_tag password_resets_path, :method => :post do %>
<div>
    <%= label_tag :email %>
    <%= text_field_tag :email, params[:email] %>
</div>
<div><%= submit_tag %></div>
<%= form_for @user do |f| %>
<p>
    <%= f.label :password %><br>
    <%= f.password_field :password %>
    <%= error_field(@user, :password) %>
</p>

<p>
    <%= f.label :password_confirmation %><br>
    <%= f.password_field :password_confirmation %>
    <%= error_field(@user, :password_confirmation) %>
</p>

<p>
    <%= f.submit %>
</p>
class UsersController < ApplicationController

before_action :can_change, only: [:edit, :update]
before_action :require_no_authentication, only: [:new, :create]

def show
    @user = User.find(params[:id])
end

def new
    @user = User.new
end

def create
    @user = User.new(user_params)

    if @user.save
        Signup.confirm_email(@user).deliver

        redirect_to new_user_sessions_path, notice: t('flash.notice.user_created') 
    else
        render action: :new
    end
end

def edit
    @user = User.find(params[:id])
end

def update
    @user = User.find(params[:id])

    if @user.update(user_params)
        flash[:notice] = t('flash.notice.user_updated')
        redirect_to @user
    else
        render action: :edit
    end
end

private

def user_params
    params.require(:user).permit(:full_name, :email, :birthday, :password, :password_confirmation, :about)
end

def can_change
    unless user_signed_in? && current_user == user
        redirect_to user_path(params[:id])
    end
end

def user
    @user ||= User.find(params[:id])
end
resource :password_resets