ruby on rails - Why is devise not displaying authentication errors on sign in page? -
i'm using rails 4.2
i have helper file called devise_helper.rb
module devisehelper def devise_error_messages! return "" if resource.errors.empty? messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join sentence = i18n.t("errors.messages.not_saved", count: resource.errors.count, resource: resource.class.model_name.human.downcase) html = <<-html <div class="row"> <div class="large-12 columns"> <div data-alert class="alert-box alert radius"> <h4>#{sentence}</h4> <ul>#{messages}</ul> </div> </div> </div> html html.html_safe end end to customize error messages , it's working registrations , passwords pages, not sessions pages. why this? know can add this:
<div class="row"> <% if notice %> <div data-alert class="alert-box info radius"> <%= notice %><%= link_to "x", '#', :class => 'close' %> </div> <% end %> <% if alert %> <div data-alert class="alert-box alert radius"> <%= alert %><%= link_to "x", '#', :class => 'close' %> </div> <% end %> </div> to application.html.erb file , error messages display there, don't understand why have add when have devise helper already. passwords , registrations, had add <%= devise_error_messages! %> sessions pages don't seem work way. i'm not sure if how devise works or if there's i'm missing.
edit: generated sessions controller never changed in since generating it. understand, devise use default controller until change 1 generated. passwords controller well. did make changes registrations controller configure permitted parameters.
class users::sessionscontroller < devise::sessionscontroller end
a login blank/wrong fields does not trigger (your) validations on model, , therefore won't show validation errors !
if debug byebug (in first line of view example), you'll notice
resource.errors.count # => 0 flash # => ....@flashes={"alert"=>"invalid email or password."} devise populates "alert flash" specific sign in error messages unique context of sign-in.
why not see model validation error messages ? because wouldn't make sense : suppose model has mandatory :gender attribute validates_presence_of :gender. if normal model errors added, see "gender cannot blank" in list of errors when user tries sign in wrong login :oops:.
devise_error_messages! specific devise method meant show specific errors. can think of partial validation on fields used sign in (and defined in devise config file)
workaround :
if want show error messages, explicitely run validations :
at beginning of devise_error_messages!
resource.validate # generate errors , populate `resource.errors` i believe shouldn't mess other actions work (register, etc.)
Comments
Post a Comment