moondaisy
moondaisy

Reputation: 4491

Devise log_in errors not showing

I have been looking at similar questions but I haven't been able to pinpoint my error.

I have this code in my application.html.erb, and it works when it comes to showing other flash messages (such as "Successful login"):

<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <div class="alert alert-dismissible alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    </div>
  <% end %>
<% end %>

But regardless of that the errors don't display on sign in for any of my models (I have 3 different models).

This is the response of the requests:

Started POST "/users/sign_in" for 127.0.0.1 at 2017-10-14 16:25:35 -0300
Processing by Users::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5yM5EkGw1WVrFsaHm39Em7JkXgu0OZb5FDeO7bJA+SEQBYBybfw7ghbK1WM5I8krbBjP/pRWLF+PG0B2k+Crkw==", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Iniciar sesión"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
Completed 401 Unauthorized in 107ms (ActiveRecord: 5.0ms)


Processing by Users::SessionsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5yM5EkGw1WVrFsaHm39Em7JkXgu0OZb5FDeO7bJA+SEQBYBybfw7ghbK1WM5I8krbBjP/pRWLF+PG0B2k+Crkw==", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Iniciar sesión"}
  Rendering users/sessions/new.html.erb within layouts/application
  Rendered users/sessions/new.html.erb within layouts/application (2.0ms)
  Rendered layouts/_navigation.html.erb (1.0ms)
  Rendered layouts/_messages.html.erb (0.5ms)
Completed 200 OK in 285ms (Views: 223.4ms | ActiveRecord: 0.0ms)

But flash is empty if user/password is wrong or if user hasn't been confirmed yet.

I have redefined my controllers to get them working correctly for multiple models, as explained in the wiki

class Users::SessionsController < Devise::SessionsController
  include Accessible
  skip_before_action :check_user, only: :destroy
end

But I haven't redefined the methods.

Upvotes: 1

Views: 1335

Answers (1)

Anton
Anton

Reputation: 550

The question was tagged with 'devise' so I assume you are using it. Devise errors are not flash messages and not implementing this mechanism. Take a look into this partial devise_error_messages!. You could include it inside your Devise form.

Update

I am updating my answer following the question updates. I've created a new app with three Devise models as described and used the code snippet from the question to generate the flashes. I've followed the tutorial from the question description and was able to reproduce your issue. It appears that the flash.clear in the concern clears the flash when user redirects back to the new session view.

You could add the following:

flash.clear unless params['action'] == 'new'

as a quick hack to prevent the flash clear on redirects back to new session page.

Upvotes: 5

Related Questions