设计:如何自定义注册控制器销毁方法

时间:2012-06-16 07:58:28

标签: ruby-on-rails-3 devise crud destroy rails-models

我的用户和个人资料分别位于不同的模型中。删除用户时,链接的配置文件仍然存在,这是所需的结果。我想要做的是将配置文件记录标记为已删除。

我在配置文件表中添加了一个已删除的列(boolean),但是无法弄清楚如何将set添加到true设置为devise destroy方法?

应用\控制器\ registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
   def destroy
     delete_profile(params) 
   end


   private

   def delete_profile(params)
     profile = Profile.find(params[:id])
     profile.deleted = true
   end  
end

但我可以弄清楚如何解决这个错误

Couldn't find Profile without an ID

如何在我的视图中从删除用户传递正确的参数?

1 个答案:

答案 0 :(得分:1)

Devise不使用params[:id]来销毁当前用户(因此不通过路由提供),而是使用current_user

以下是控制器的相关部分:

class Devise::RegistrationsController < DeviseController
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

  def destroy
    resource.destroy
    Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
    set_flash_message :notice, :destroyed if is_navigational_format?
    respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name)       }
  end

  protected 

  def authenticate_scope!
    send(:"authenticate_#{resource_name}!", :force => true)
    self.resource = send(:"current_#{resource_name}")
  end
end

所以你的替代方案是做

之类的事情
class RegistrationsController < Devise::RegistrationsController
  def destroy
    current_user.deleted = true
    current_user.save
    #some more stuff
  end
end