gpt4 book ai didi

ruby-on-rails - Destroy_with_password 总是返回 false

转载 作者:行者123 更新时间:2023-12-04 10:30:07 27 4
gpt4 key购买 nike

使用 Ruby gem Devise 的 destroy_with_password 构建 walks through how to require a password to delete a user account 的现有问题。

我的销毁操作如下所示:

def destroy
@user = User.find(current_user.id)
if @user.destroy_with_password(user_params)
redirect_to root_url, notice: "User deleted."
else
redirect_to root_url
flash[:notice] = "Couldn't delete"
end
end

def user_params
params.require(:user).permit(:first_name, :last_name, :password,
:password_confirmation, :current_password... (etc) )
end

和形式:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :delete }) do |f| %>
<%= f.password_field :current_password, autocomplete: "off" %>
<%= f.button :submit %>
<% end %>

重定向有效,但即使在不删除帐户的情况下输入了正确的密码,它也会重定向。

我运行 binding.pry ,并且 @user.destroy_with_password(user_params) 总是返回 false。

如果我打开 user_params 这是返回的内容:
<ActionController::Parameters {"current_password"=>"123456"} permitted: true>

最佳答案

根据官方文档 destroy_with_password接受 current_password参数,而不是传递整个 user_params只需通过 current_password , 做这个

@user.destroy_with_password(user_params[:current_password])

rubydoc 中的更多信息

Devise::Models::DatabaseAuthenticatable#destroy_with_password

试一试。

关于ruby-on-rails - Destroy_with_password 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60454812/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com