Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise - forgot password not showing error message if the email does not exist in the db

When a users clicks forgot password, it takes them to the forgot password page with a field for them to input their email address. If their email address is in the database, everything is peachy, but if the email address does NOT exist in the database, it just redirects to the same page, but does not show an error message.

How can I get the error message to appear?

/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 %>
like image 475
Catfish Avatar asked Oct 31 '13 22:10

Catfish


1 Answers

In your shared messages partial you are only displaying errors from the controllers. You need to include errors that are attached to your model. In this case the error message is attached to the User model. You /views/shared/_messages.html.erb should actually look like:

<% 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 %>

and while rendering the partial, you need to pass in the model.

<%= render '/shared/messages', model: resource %>
like image 142
Anurag Abbott Avatar answered Oct 14 '22 20:10

Anurag Abbott