2012-01-04 29 views
4

我已經成功地實施了這些鏈接,登陸失敗後的輪廓自定義重定向...設計重定向登錄後無法使用閃光燈消息

Devise redirect after login fail https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

但是我沒有在得到適當的提示信息我的靜態頁面。我試圖在這樣的custom_failure.rb加入了閃光燈消息...

def redirect_url 
    login_path 
    flash[:notice] = "Invalid login or password" 
end 

...但沒有雪茄。理想情況下,我想顯示默認的設計錯誤消息。任何方式來做到這一點?

...我還在我的應用程序的靜態頁面中顯示我的登錄和註冊頁面。因此,對於在應用程序/視圖/ static_pages/login.html.erb我有實例化......

<%= form_for("user", :url => user_session_path) do |f| %> 
<table> 
<tr> 
<td><%= f.label :email %></td> 
<td><%= f.text_field :email %></td> 
</tr> 
<td><%= f.label :password %></td> 
<td><%= f.password_field :password %></td> 
</table> 
<%= f.check_box :remember_me %> 
<%= f.label :remember_me %><p/> 
<%= f.submit 'Sign in' %><p/> 
<%= link_to "Forgot your password?", new_password_path('user') %> 
<% end %> 

測試

回答

3

我最終將這些行添加到我的佈局文件,它的工作原理。感謝馬里奧的幫助。

<%= content_tag(:div, flash[:error], :id => "flash_error") if flash[:error] %> 
<%= content_tag(:div, flash[:notice], :id => "flash_notice") if flash[:notice] %> 
<%= content_tag(:div, flash[:alert], :id => "flash_alert") if flash[:alert] %> 
2

裏面您的自定義應用程序失敗,你應該能夠在respond設定閃光消息方法如下:

class CustomFailure < Devise::FailureApp 
    def redirect_url 
    login_path 
    end 

    # You need to override respond to eliminate recall 
    def respond 
    if http_auth? 
     http_auth 
    else 
     flash[:notice] = I18n.t(:unauthenticated, :scope => [ :devise, :failure ]) 
     redirect 
    end 
    end 
end 
+0

感謝您的回覆馬里奧。我嘗試了你的建議,但確實奏效。你能想出任何原因嗎?感謝名單! – Lumbee 2012-01-04 21:04:08

+0

不是真的,您的應用程序是否使用您創建的自定義失敗類?你可以嘗試在響應方法中加一個加號以確保代碼正在運行嗎? – 2012-01-05 01:43:50

+0

嘿,馬里奧,看了這一些後,我認爲這個問題是在視圖中。我不需要在視圖中顯示閃光信息的東西嗎?這裏是我的觀點看起來像哪個是app/views/static_pages/login.html.erb https://gist.github.com/1565343 – Lumbee 2012-01-05 13:50:27