2012-06-16 28 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