2012-06-16 106 views
0

我有我的用户和配置文件在不同的模型。当用户被删除时,链接的配置文件将保留,这是所需的结果。我想要做的是将配置文件记录标记为已删除。设计:如何自定义注册控制器销毁方法

我已经添加了一个删除列(布尔)到我的个人资料表,但无法弄清楚如何将设置添加到true设置为设计销毁方法?

应用程序\控制器\ 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 

我怎么能在正确的PARAMS通过从用户删除我的看法?

+0

你有'destroy'方法名错字 – NARKOZ

+0

谢谢,我已经更新我的代码 –

回答

1

设计不使用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 
+0

谢谢,我结束了使用: \t \t current_user.profile.update_attribute(:deleted,true) \t \t超级会达到同样的效果吗? –

+0

可以肯定的是,您用于身份验证的Devise模型是什么?它是用户还是配置文件?在我的示例中,我使用了'current_user'方法,但这应该是'current _#{devise_resource}',其中devise_resource为'user'或'profile'。将它传递给'super'时要小心,这会在完成自定义工作后触发默认操作,通常会真正删除资源。 – niels

相关问题