2012-01-06 47 views
2

我有一堆代碼將登錄用戶的客人/懶惰註冊帳戶中的內容交給他創建新會話時運行的新帳戶。設計:確認後運行代碼

class SessionsController < Devise::SessionsController 

    def new 
    super 
    end 

    def create 
    super 
    logging_in # this is the method which will run 
    end 

    def destroy 
    super 
    end 

end 

它在用戶登錄時起作用。但是,如果Devise在確認後記錄用戶,則上述內容不會運行。如果我希望在用戶登錄後運行該方法,我應該在哪裏放置該方法?無論是通過登錄還是確認。

+1

你可以嘗試重新定義這個方法:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb#L19 – 2012-01-06 11:12:59

回答

6

謝謝納什。這是我做到的。

class ConfirmationsController < Devise::ConfirmationsController 

    def new 
    super 
    end 

    def create 
    super 
    end 

    def show 
    self.resource = resource_class.confirm_by_token(params[:confirmation_token]) 

    if resource.errors.empty? 
     set_flash_message(:notice, :confirmed) if is_navigational_format? 
     sign_in(resource_name, resource) 
     logging_in # Here it is 
     respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } 
    else 
     respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new } 
    end 
    end 

    protected 

    def after_resending_confirmation_instructions_path_for(resource_name) 
     new_session_path(resource_name) 
    end 

    def after_confirmation_path_for(resource_name, resource) 
     after_sign_in_path_for(resource) 
    end 

end 

它需要因爲我logging_in方法使用current_usersign_in後加入。

+0

只需要注意一下,如果你要擴展Devise的控制器,你不必重寫'new'和'create'就可以調用'super'。你可以發射它們,那會自動發生。 – n0nick 2012-10-25 11:44:53