4

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

Community
  • 1
  • 1
Mark Locklear
  • 5,044
  • 1
  • 51
  • 81

2 Answers2

3

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] %>
Mark Locklear
  • 5,044
  • 1
  • 51
  • 81
2

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
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Mario Visic
  • 2,653
  • 1
  • 22
  • 29
  • Thanks for the response Mario. I tried you suggestion but it did work. Can you think of any reason why? Thanx! – Mark Locklear Jan 04 '12 at 21:04
  • Not really, is your app definitely using the custom failure class you've created? Can you try putting a raise in the respond method to make sure the code is being run? – Mario Visic Jan 05 '12 at 01:43
  • Hey Mario, after looking at this some more I think the issue is in the view. Don't I need something in the view that will display the flash message? Here is what my view looks like which is app/views/static_pages/login.html.erb https://gist.github.com/1565343 – Mark Locklear Jan 05 '12 at 13:50
  • Interesting. I find when I drop ' <%= flash[:notice] = I18n.t(:unauthenticated, :scope => [ :devise, :failure ]) %>' into the view I get the message "You need to sign in or sign up before continuing" So this is definitely coming from Devise, but this is the only message I get. If I enter a wrong password I do not get a different message. – Mark Locklear Jan 05 '12 at 18:57
  • Oh right, yes that is the devise/failure message, You can change it to something else, or check the existing devise translations here. https://github.com/plataformatec/devise/blob/master/config/locales/en.yml – Mario Visic Jan 06 '12 at 05:50