Ruby-on-rails – Devise redirect after login fails with flash message

deviseruby-on-rails

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

Best Answer

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] %>
Related Topic