Destroy_with_password always returns false

Deadly 提交于 2021-01-28 20:12:00

问题


Building off of an existing question that walks through how to require a password to delete a user account using the Ruby gem Devise's destroy_with_password.

My destroy action looks like this:

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

and the form:

<%= 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 %>

The redirect works, but it redirects even when the correct password is inputted without deleting the account.

I run binding.pry, and @user.destroy_with_password(user_params) always returns false.

If I open user_params this is what is returned:

<ActionController::Parameters {"current_password"=>"123456"} permitted: true>

回答1:


According to the official docs destroy_with_password accepts current_password argument, so instead of passing the whole user_params just pass the current_password, do this

@user.destroy_with_password(user_params[:current_password])

More info in rubydoc

Devise::Models::DatabaseAuthenticatable#destroy_with_password

Give it a try.



来源:https://stackoverflow.com/questions/60454812/destroy-with-password-always-returns-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!