-1

everything is working fine apart from the destroy method for my user model. I'm FAIRLY sure now that it has something to do with the Friendly_ID gem.

If I require a password then i get an error of invalid password. If I don't require a password then the User 'apparently' deletes, but it doesn't. (my flash[:success] shows with the corresponding redirect)

Server logs:

Processing by UsersController#destroy as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"SkfqooVdKgfsgoL78+yZDxSYoTJNqzUfmyWfGTv8LOxSEObnaKEhip837yrEK5bHVO0HJ/6dmKHqUZmiiWoqDg==", "user"=>{"current_password"=>"[FILTERED]"}, "commit"=>"Delete", "locale"=>"en", "id"=>"e2dc"}

Here the ID is shown as "e2dc" instead of the real ID of 20. (e2dc is the username and slug of this current user)

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :username, use: [:slugged, :finders]
end

class UsersController < ApplicationController

  def destroy
    @user = current_user
    @user.id = params[:user_id]
    if @user.destroy_with_password(user_params)
        redirect_to feedbacks_url, notice: "User deleted."
    else
      render "account"
      flash.now[:notice] = @user.errors.full_messages
    end
  end
end

<%= simple_form_for(@user, :method => :delete) do |f| %>
  <%= f.input :current_password, autocomplete: "off", required: true %>
  <%= f.button :submit, "Delete" %>
<% end %>

I have tried using User.friendly.find without any luck. EVERYTHING else works in my project.

Thanks for any advice

Dharam Gollapudi
  • 6,328
  • 2
  • 34
  • 42
Rob Hughes
  • 876
  • 2
  • 13
  • 32

1 Answers1

1

Try the following:

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

  if @user.destroy_with_password(params[:user][:current_password])
    redirect_to feedbacks_url, notice: "User deleted."
  else
    flash.now[:notice] = @user.errors.full_messages
    render "account"  
  end
end
Dharam Gollapudi
  • 6,328
  • 2
  • 34
  • 42
  • This works perfectly, although I don't understand why. With every other method in my controller, (I even have an update method in the same controller that users `update_with_password(user_params)` I can user `@user = current_user` or `find.(params[:id])`. Thank you anyway!! – Rob Hughes Sep 07 '16 at 18:10