4
votes

I have successfully implemented a custom redirect after a failed login outline in these links...

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

However I am not getting the appropriate flash messages in my static page. I tried adding a flash message in the custom_failure.rb like this...

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

...but no cigar. Ideally I would like to display the default devise error messages. Any way to do this?

...also I am displaying my login and registration pages in static pages in my app. So for instanced in app/views/static_pages/login.html.erb I have...

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

test

2

2 Answers

3
votes

I ended up adding these lines to my layout file, and it works. Thanks for the help Mario.

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

Inside your custom failure app, you should be able to set the flash message in the respond method like so:

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