重置密码时,更新功能在 rails 后停止工作
When resetting password, update function stops working in rails
这是我的代码:
password_resets_controller.rb
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update]
def new
end
def create
# @user = User.find_by(email: params[:password_resets] [:email].downcase)
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def update
if params[:member][:password].empty?
@user.errors.add(:password, "Can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
# log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:member).permit(:password, :password_confirmation)
end
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless @user
redirect_to root_url
end
end
# Check expiration of reset token.
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
第三种情况是我进入密码重置令牌的路径后,我输入了新密码和password_confirmation。然后我点击更新。但是,此错误页面显示。我真的不知道怎么解决这个问题。
这是我的编辑文件:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages' %>
<%= hidden_field_tag :email, @user.email %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
</div>
这是日志:
这是我的用户 table:
这是将 password_reset_path(params[:id]) 更改为 password_reset_path(@user) 后的日志
问题是因为您的 form
发送了 member
密钥而不是 user
,如您的日志所示:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"...",
"email"=>"naomi.wuuu@gamil.com",
"member"=>{
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"
},
...
}
因此,由于没有 user
键,params[:user]
returns nil
并且由于它无法响应 []
,导致错误:
undefined method `[]' for nil:NilClass
查看您的控制器和视图,我不明白为什么使用 member
而不是 user
,但您可以强制 form
发送 user
使用 as: "user"
键,像这样:
<%= form_for(@user, url: password_reset_path(params[:id]), as: "user") do |f| %>
有了这个,您的参数现在将是:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"...",
"email"=>"naomi.wuuu@gamil.com",
"user"=>{
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"
},
...
}
这是我的代码: password_resets_controller.rb
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update]
def new
end
def create
# @user = User.find_by(email: params[:password_resets] [:email].downcase)
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def update
if params[:member][:password].empty?
@user.errors.add(:password, "Can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
# log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:member).permit(:password, :password_confirmation)
end
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless @user
redirect_to root_url
end
end
# Check expiration of reset token.
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
第三种情况是我进入密码重置令牌的路径后,我输入了新密码和password_confirmation。然后我点击更新。但是,此错误页面显示。我真的不知道怎么解决这个问题。
这是我的编辑文件:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages' %>
<%= hidden_field_tag :email, @user.email %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
</div>
这是日志:
这是我的用户 table:
这是将 password_reset_path(params[:id]) 更改为 password_reset_path(@user) 后的日志
问题是因为您的 form
发送了 member
密钥而不是 user
,如您的日志所示:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"...",
"email"=>"naomi.wuuu@gamil.com",
"member"=>{
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"
},
...
}
因此,由于没有 user
键,params[:user]
returns nil
并且由于它无法响应 []
,导致错误:
undefined method `[]' for nil:NilClass
查看您的控制器和视图,我不明白为什么使用 member
而不是 user
,但您可以强制 form
发送 user
使用 as: "user"
键,像这样:
<%= form_for(@user, url: password_reset_path(params[:id]), as: "user") do |f| %>
有了这个,您的参数现在将是:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"...",
"email"=>"naomi.wuuu@gamil.com",
"user"=>{
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"
},
...
}