Ruby on Rails - Friendly-ID - Id在destroy方法

时间:2016-09-07 16:41:21

标签: ruby-on-rails devise destroy friendly-id

除了我的用户模型的destroy方法之外,一切正常。我现在很确定它与Friendly_ID gem有关。

如果我需要密码,则会收到invalid password的错误。 如果我不需要密码,那么User'显然会'删除',但事实并非如此。 (我的flash[:success]显示相应的重定向)

服务器日志:

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

此处ID显示为“e2dc”而不是实际ID 20.(e2dc是当前用户的用户名和slug)

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

我尝试过使用User.friendly.find而没有任何运气。其他一切都适用于我的项目。

感谢您的任何建议

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

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