Force redirection after login failure

I have successfully completed custom redirects after an unsuccessful login scheme for these links ...

Configure redirection after failed login https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

However, I do not receive the corresponding flash messages on my static page. I tried adding a flash message to custom_failure.rb like this ...

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

... but there is no cigar. Ideally, I would like to display error messages by default. Any way to do this?

... also I display my login and registration pages on static pages in my application. 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

+4
source share
2 answers

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] %> 
+3
source

Inside the user application with an error, you must set the flash message in the respond method as follows:

 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 
+2
source

Source: https://habr.com/ru/post/1389284/


All Articles