2013-06-01 71 views
0

我使用Devise的此guide來設置用戶無需密碼即可創建帳戶的功能,但可以在稍後進行確認時進行設置。從Devise確認更改閃存錯誤

但是錯誤正在通過<%= devise_error_messages! %>摘錄顯示。我想更換控制器,因此它顯示在來自Rails的通常的flash上。

我該怎麼做?

我重寫的控制器如下:

# app/controllers/confirmations_controller.rb 
class ConfirmationsController < Devise::ConfirmationsController 

    layout "login" 

    # Remove the first skip_before_filter (:require_no_authentication) if you 
    # don't want to enable logged users to access the confirmation page. 
    skip_before_filter :require_no_authentication 
    skip_before_filter :authenticate_user! 

    # PUT /resource/confirmation 
    def update 
    with_unconfirmed_confirmable do 
     if @confirmable.has_no_password? 
     @confirmable.attempt_set_password(params[:user]) 
     if @confirmable.valid? 
      do_confirm 
     else 
      do_show 
      @confirmable.errors.clear #so that we wont render :new 
     end 
     else 
     self.class.add_error_on(self, :email, :password_allready_set) 
     end 
    end 

    if [email protected]? 
     render 'devise/confirmations/new' #Change this if you don't have the views on default path 
    end 
    end 

    # GET /resource/confirmation?confirmation_token=abcdef 
    def show 
    with_unconfirmed_confirmable do 
     if @confirmable.has_no_password? 
     do_show 
     else 
     do_confirm 
     end 
    end 
    if [email protected]? 
     self.resource = @confirmable 
     render 'devise/confirmations/new' #Change this if you don't have the views on default path 
    end 
    end 

    protected 

    def with_unconfirmed_confirmable 
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, params[:confirmation_token]) 
    if [email protected]_record? 
     @confirmable.only_if_unconfirmed {yield} 
    end 
    end 

    def do_show 
    @confirmation_token = params[:confirmation_token] 
    @requires_password = true 
    self.resource = @confirmable 
    render 'devise/confirmations/show' #Change this if you don't have the views on default path 
    end 

    def do_confirm 
    @confirmable.confirm! 
    set_flash_message :notice, :confirmed 
    sign_in_and_redirect(resource_name, @confirmable) 
    end 
end 

回答

2

我認爲你可能有錯誤信息,混淆了閃光燈的消息。當您通過<%= devise_error_messages! %>顯示錯誤時,這些錯誤消息來自模型上設置的驗證。例如在User模型,

validates :password, presence: true 

這些是相對於閃光消息不同的錯誤。您可以在控制器中設置flash此類郵件

def create 
    # code ..... 
    if @post.save 
    flash[:success] = "Post was successfully created" 
    redirect_to [@investigation, @post] 
    else 
    flash.now[:error] = "Post was not created" 
    render 'new' 
    end 
end 

這些閃光的消息只是爲了你的控制器動作中指定的條件作出反應。希望這可以幫助