Do not show login error messages

I use the application for my application and I do not receive error messages when login fails.

I have flash [: notice] and flash [: alert] on the login page.

I tried a lot of things, and when I remove "protect_from_forgery" from the application controller, I get error messages.

I also use Cancan in my application, can this be some kind of problem? Any ideas?

thanks

+6
source share
2 answers

I assume authentication does not work. Do your forms submit token authenticity with messages? If removing protect_from_forgery fixes it, it's almost certainly a problem.

Make sure that all requests without request send the authenticity_token parameter with the value returned by the rails form_authenticity_token function. If you use form_for in your views, this should happen automatically. Check your html to make sure that the authentication token in the form must match the value returned by the form_authenticity_token method.

+2
source

It may be a bit hacked, but I use this helper (app / helpers / devise_helper.rb) to capture the flashes and use the ones that are installed, and then by default resource.errors . The developer of the session controller does not seem to use model errors, but instead uses flash warnings. It is simply based on the helper, which is in lib development.

 module DeviseHelper def devise_error_messages! flash_alerts = [] error_key = 'errors.messages.not_saved' if !flash.empty? flash_alerts.push(flash[:error]) if flash[:error] flash_alerts.push(flash[:alert]) if flash[:alert] flash_alerts.push(flash[:notice]) if flash[:notice] error_key = 'devise.failure.invalid' end return "" if resource.errors.empty? && flash_alerts.empty? errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages messages = errors.map { |msg| content_tag(:li, msg) }.join sentence = I18n.t(error_key, :count => errors.count, :resource => resource.class.model_name.human.downcase) html = <<-HTML <div id="error_explanation"> <h2>#{sentence}</h2> <ul>#{messages}</ul> </div> HTML html.html_safe end end 
+1
source

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


All Articles