2012-10-15 34 views
3

我設置Devise,使用戶可以登錄和使用該網站,而不必確認他們的電子郵件地址,類似於此question。但是網站上有一些功能,除非用戶確認,否則用戶無法使用。設計:當重新發送確認電子郵件,更改閃光消息

好的,沒關係。我可以檢查current_user.confirmed?。如果他們沒有確認,我可以在頁面上放一個按鈕讓他們請求再次發送確認。

我遇到的問題是當他們在登錄時執行此操作時,他們在結果頁面上看到的Flash消息是「您已經登錄」。哪一個不理想 - 我只想提出確認已發送的消息。

我開始嘗試找出Devise::ConfirmationController的哪一種方法來覆蓋哪些方法,但我希望有人已經這樣做了。

回答

6

原因閃光說是因爲用戶正在從after_resending_confirmation_instructions_path_for方法重定向到new_session_path「你已登錄」。我會覆蓋這個方法來檢查他們是否登錄。如果他們是,那麼不要重定向到new_session_path,設置你的Flash消息並重定向到另一個頁面。

,將其置於controllers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController 

    protected 

    def after_resending_confirmation_instructions_path_for(resource_name) 
    if signed_in? 
     flash[:notice] = "New message here" #this is optional since devise already sets the flash message 
     root_path 
    else 
     new_session_path(resource_name) 
    end 
    end 
end 

重寫確認控制器添加您confirmationsController到routes->

devise_for :users, :controllers => {:confirmations => 'users/confirmations' } 
+0

獲勝者獲勝者!奇蹟般有效。謝謝! – dpassage

+0

很高興我能幫忙!我碰巧正在處理相同的用例。 – flynfish

+0

我稍微改變了這個,使用下面的代碼來設置來自翻譯文件的flash消息,而不是:'set_flash_message(:notice,:send_instructions)' – dpassage

1

我認爲它應該是這個樣子:

module Devise 
    module ConfirmationsController 
    extend ActiveSupport::Concern 

    included do 
     alias_method_chain :show, :new_flash 
    end 

    def show_with_new_flash 
     # do some stuff 
     flash[:notice] = "New message goes here" 
    end 
    end 
end 
+0

每https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb,'ConfirmationsController'是不是一個模塊,這是一個類。你自己做過嗎? – dpassage

+0

我做了它,但在1年的舊項目,不知道它是否會與最新的紅寶石/寶石版本。 – nexo

0

可以編輯

配置/區域設置/ devise.en.yml到在線更加相關:

failure: 
    already_authenticated: 'You are already signed in.' 

或者你可以在你的視圖,其中閃存的消息已被添加

<%=content_tag :div, msg, id: "flash_#{name}" unless msg.blank? or msg == "You are already signed in."%> 
+0

這不完全正確,因爲那樣當用戶確實嘗試登錄兩次時,錯誤也會發生變化。 – dpassage

0
我使用的設計3.1.0

做到這一點,有這種情況的,而不是after_resending_confirmation_instructions_path_for在上面描述的不同的方法,投票的答案。我修改我的,像這樣:

class Users::ConfirmationsController < Devise::ConfirmationsController 

    protected 

    def after_confirmation_path_for(resource_name, resource) 
    if signed_in? 
     set_flash_message(:notice, :confirmed) 
     root_path 
    elsif Devise.allow_insecure_sign_in_after_confirmation 
     after_sign_in_path_for(resource) 
    else 
     new_session_path(resource_name) 
    end 
    end 
end 
相關問題