2013-10-31 27 views
3

當用戶點擊忘記的密碼時,它會將其帶到忘記的密碼頁面,其中有一個字段供他們輸入其電子郵件地址。如果他們的電子郵件地址在數據庫中,則一切都很好,但如果電子郵件地址不存在於數據庫中,它只會重定向到同一頁面,但不會顯示錯誤消息。設計 - 如果電子郵件不存在於分區中,則忘記密碼不會顯示錯誤消息

如何獲取錯誤消息?

/view/devise/passwords/new.html.erb

... 
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %> 
    <h1>Reset Password</h1>  

    <div class="login-fields"> 

     <p>Instructions on resetting your password will be emailed to you.</p> 

     <%= render :partial => '/shared/messages' %> 

     <div class="field"> 
      <%= f.label :email %> 
      <%= f.email_field :email, :placeholder => 'Email', :class => 'login username-field' %> 
     </div> <!-- /field --> 

    </div> <!-- /login-fields --> 

    <div class="login-actions"> 

     <%= content_tag(:button, :type=>:submit, :class => "button btn btn-secondary btn-large") do %> 
      Send me reset password instructions 
     <% end %> 

    </div> <!-- .actions --> 

    <div class="login-social"> 
     <%= render "devise/shared/links" %> 
    </div> 

<% end %> 
... 

/views/shared/_messages.html.erb

<% if alert || flash[:alert] || flash[:error] %> 
    <div class="alert alert-error"> 
     <a class="close" data-dismiss="alert" href="#">x</a> 
     <h4 class="alert-heading">Error!</h4> 
     <%= alert %> 
     <%= flash[:error] %> 
    </div> 
<% end %> 

<% if flash[:success] || notice || flash[:notice]%> 
    <div class="alert alert-success"> 
     <a class="close" data-dismiss="alert" href="#">x</a> 
     <h4 class="alert-heading">Success!</h4> 
     <%= flash[:success] %> 
     <%= notice %> 
    </div> 
<% end %> 
+0

是否有任何HTML被渲染?即它顯示「錯誤!」標題但不是實際的消息?或者它什麼都沒有顯示? – Flambino

+3

請注意,這是將信息暴露給潛在的攻擊者。有人試圖猜測系統中有效的電子郵件地址,可以根據他們是否收到錯誤消息來判斷某人是否擁有賬戶。如果[您的登錄表單顯示用戶名或密碼不正確],則顯示的信息類似(https://www.informit.com/articles/article.aspx?p=1076787&seqNum=6)。您需要平衡安全性和針對您的特定應用程序的可用性。 – carols10cents

回答

6

在部分共享消息中,您只顯示來自控制器的錯誤。您需要包含附加到模型的錯誤。在這種情況下,錯誤消息被附加到用戶模型。您/views/shared/_messages.html.erb實際上應該是這樣的:

<% if alert || flash[:alert] || flash[:error] %> 
    <div class="alert alert-error"> 
     <a class="close" data-dismiss="alert" href="#">x</a> 
     <h4 class="alert-heading">Error!</h4> 
     <%= alert %> 
     <%= flash[:error] %> 
    </div> 
<% end %> 
<% if model.errors.any? %> 
    <div class="alert alert-error"> 
     <a class="close" data-dismiss="alert" href="#">x</a> 
     <h4 class="alert-heading">Error!</h4> 
     <ul> 
     <% model.errors.full_messages.each do |msg| %> 
      <li>* <%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
<% end %> 
<% if flash[:success] || notice || flash[:notice]%> 
    <div class="alert alert-success"> 
     <a class="close" data-dismiss="alert" href="#">x</a> 
     <h4 class="alert-heading">Success!</h4> 
     <%= flash[:success] %> 
     <%= notice %> 
    </div> 
<% end %> 

,並在渲染部分,你需要在模型中通過。

<%= render '/shared/messages', model: resource %> 
+0

謝謝你指出這一點。實際上我有另外一個部分用於模型錯誤,但沒有意識到我在這裏錯過了它。 – Catfish

+0

謝謝。這個答案很有幫助,但在我的情況下,我必須檢查資源是否真正定義了。而不是<%如果model.errors.any? %>'和'<%= render'/ shared/messages',model:resource%>',我只是在我的部分內容中檢查了「resource」。 '<%resource.errors。full_messages.each.each do | message | %>'並沒有改變'render',即'<%= render'/ shared/messages'%>' – kevinsapp

0

的形式是在password_path調用create方法。確保create.html.erb顯示錯誤以及成功(如果存在)的閃光消息。

1

如果你看一下附帶設計出默認passwords/new.html.erb視圖,你會看到,它包括對

<%= devise_error_messages! %>

電話您可以使用相同的助手在你看來,如果你喜歡將附加到用戶對象的錯誤消息(正如Anurag的答案中指出的那樣,消息附加到模型對象而不是閃光燈中)。

這個助手將返回標記,看起來像:

<div id="error_explanation"> 
    <h2>1 error prohibited this user from being saved</h2> 
    <ul><li>email not found</li></ul> 
</div> 

,如果你想定製這個,你可以這樣做很容易。只是不要使用助手,而是添加自己的自定義標記訪問resource.errors.full_messages以獲取「未找到電子郵件」消息。或者老實說,因爲這是你要在這個頁面上遇到的唯一錯誤,你可以這樣做:

<% unless resource.errors.empty? %> 
    <div>email not found</div> 
<% end %> 
相關問題