Destroy_with_password 总是 returns 错误

Destroy_with_password always returns false

使用 Ruby gem Devise 的 destroy_with_password.

建立现有问题

我的销毁动作是这样的:

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) 总是 returns 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])

更多信息在 ruby​​doc

Devise::Models::DatabaseAuthenticatable#destroy_with_password

试一试。