2012-06-19 43 views
1

設計行爲奇怪。它顯示紅色或錯誤消息,但不顯示綠色或成功消息。設計不顯示成功消息

這是在我根據用戶是否登錄的情況對路由和控制器進行更改以重定向到登錄頁面或主頁之後發生的。

所以routes.rb中現在有:

devise_for :users 

root :to => "questions#redirect_on_visit" 

match 'home', :to => "questions#index" 

和控制器:

before_filter :authenticate_user!, :except => [:redirect_on_visit] 

def redirect_on_visit 
    if user_signed_in?  
    redirect_to home_path  
    else  
    redirect_to new_user_session_path  
    end 
end 

也改變了應用程序控制器之後登錄/註銷重定向到相應的頁面:

protected 

def after_sign_in_path_for(resource)  
    stored_location_for(:user) || root_path  
end 

private 

def after_sign_out_path_for(resource) 
    stored_location_for(:user) || root_path  
end 
+1

當你登錄或註銷時,你對閃存對象有什麼看法我想設計把這些信息寫成'flash [:notice]'而不是'flash [:success]' – Viren

+0

是的!它當然是'flash [:notice]'。通過成功消息我的意思是說你已經登錄或退出成功的消息。設計用綠色背景標記它們。 – Humming

+0

我很確定背景顏色,但如果你關心的是flash消息檢查'devise.yml'(在'config/locales'目錄下)文件會給你更多的洞察吧 – Viren

回答

3

我被更好非常麻木。我有root :to => "questions#redirect_on_visit"這個事實,會在用戶每次訪問,登錄或註銷時重定向。重定向顯然是沖走了閃光消息。

最後我找到了我正在尋找的解決方案。如here所述,保留flash消息flash.keep(:notice)

0

你的問題是從多重定向login => root => h青梅 作爲flash對象都有一個生命只留下了一個請求

我想這將是寫在after_sign_in_path_forredirect_on_visit路由和after_sign_out_path_for

像這樣

def after_sign_in_path_for(resource)  
    stored_location_for(:user) || home_path  
end 

private 

def after_sign_out_path_for(resource) 
    stored_location_for(:user) || new_user_session_path 
end 
+0

不,你錯了。 'redirect_on_visit'在用戶登錄之前就可以工作,而這兩種方法在用戶登錄或退出之後工作。 – Humming

+1

@Humming也許你沒有得到我的意思你問題是當設計認證一個用戶它重定向用戶'root_path'在你的情況下它是'questions#redirect_on_visit'現在當重定向發生在root_path直到那時閃存信息存在但是因爲你使用'redirect_on_visit'中的'user_signed_in?'來檢查登錄用戶,然後將它重定向到'home'頁面,你的flash信息會丟失,並且在redirect_on_visit的if語句中添加一個'debugger' '檢查'flash內容'你會看到那裏的設計信息,因爲你正在重定向 – Viren

+1

@將它弄到home_path你的flash信息在那個轉換過程中丟失了bcoz正如我之前說過的那樣,因爲flash信息只以單個請求爲準 – Viren