Search code examples
ruby-on-railsdevisedestroyfriendly-id

Ruby on Rails - Friendly-ID - Id is processed as slug on destroy method


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


Solution

  • 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