1

I use Devise for my Logins. Everything works fine with one exception. If the user enters wrong or no values on the Login page nothing happended. No Error or Error Message (Login with the correct data work fine).

I have this problem only on the /session/new page.

This is my view

.container


.row.text_content_top
    .alert.alert-danger
      %button.close{ type: "button", "data-dismiss" => "alert"} ×
      = devise_error_messages!
  .row.text_content_top
    .col-md-4.col-md-offset-4
      %h2 Sign in
  .row.text_content
    .col-md-4.col-md-offset-4.well
      = form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
        = devise_error_messages!
        .input-group.has-feedback
          %span.input-group-addon
            %i.fa.fa-envelope-o.fa-fw
          = f.label :email, class: 'sr-only'
          = f.email_field :email, autofocus: true, class: 'form-control', placeholder: 'E-Mail'
        .input-group.has-feedback.top-buffer-10
          %span.input-group-addon
            %i.fa.fa-key.fa-fw
          = f.label :password, class: 'sr-only'
          = f.password_field :password, autocomplete: 'off', class: 'form-control', placeholder: 'Password'
        = f.submit "Login", class: 'btn btn-success btn-large top-buffer-10'
      = link_to "Forgot your password?", new_password_path(resource_name), class: 'top-buffer-10'

And my model

class User < ActiveRecord::Base
  has_many :orders

  validates :email, presence: true, uniqueness: true

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable

end

I have no code in the controller or other custom code

Thx in advance

ThreeFingerMark
  • 989
  • 3
  • 12
  • 21

1 Answers1

1

In light of @JKen13579's answer, you'll need to note that Devise doesn't store error messages for login in the traditional way (using the errors object). Instead, Devise uses the flash, which is what sets some people off.


There is a way to integrate Devise error messages into your app, using this tutorial:

For devise you need to override the way devise handles flash messages. Create a file called devise_helper in "app/helpers/devise_helper.rb".

Inside the file you have to create a method called devise_error_messages!, which is the name of the file that tells devise how to handle flash messages.

module DeviseHelper
  def devise_error_messages!
    return '' if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    html = <<-HTML
    <div class="alert alert-error alert-block"> <button type="button"
    class="close" data-dismiss="alert">x</button>
      #{messages}
    </div>
    HTML

    html.html_safe
  end
end

<%= devise_error_messages! %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147