Rails - Devise - ¿Mensajes de error al iniciar sesión?

¿Cómo puedo hacer f.error_messages work here, or should I use flashes?
If so, what should override in the sessions_controller?

<h2>Create an account</h2>    
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
  <%= f.error_messages %>
    <%= f.label :email %><br />
    <%= f.text_field :email, :class => :big %>
    <%= f.label :password %><br />
    <%= f.password_field :password, :class => :big %>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, :class => :big %>

  <p><%= f.submit "Create", :class => :submit %></p>
<% end %>

PS. f.error_messages for Creating an account works totally fine.

preguntado el 08 de enero de 11 a las 20:01

5 Respuestas

Admittedly, a bit hacky, but I'm using this helper (app/helpers/devise_helper.rb) to grab flashes and use those if set then default to resource.errors.

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'

    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">



respondido 30 nov., 11:02

Hacky? You are too modest, this is really helpful. - James McMahon

try putting these in your layout:

<%= 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] %>

Login action in Devise sets flash messages, not model Errors.

Respondido el 09 de enero de 11 a las 20:01

Ok. :notice works fine after you login, but error/alert doesn't seem to get set at all.. - Frexuz

Why does it set flash messages instead of model errors? I would expect model errors given its failing validations. - pinguino

You saved me ! Thank you - Pierre

2016 and this works just like it did back in 2011. So the question here is this: if you place flash messages in your layout, how do you force them to appear between <h2>Sign in</h2> y <form>...</form> on sign in page? - mzrnsh

Despite the age of this post I wanted to share a solution to help people such as myself who had trouble when they began using Devise. To keep things DRY I just ended up inserting this code in my application.html.erb archivo:

  <% flash.each do |key, value| %>
    <div class="flash <%= key %>"><%= value %></div>
  <% end %>

  <%= yield %>

Respondido 13 Oct 14, 02:10

Incluso podrías usar <%= content_tag :div, value, class: "flash #{key}" %> :) - Frexuz

This shall also do

<% flash.each do |name, msg| %>
  <%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>

Respondido 13 Feb 16, 08:02

Create a helper

 # app/helpers/application_helper.rb

    module ApplicationHelper

      def flash_class(level)
        case level
          when 'info' then "alert alert-info"
          when 'notice','success' then "alert alert-success"
          when 'error' then "alert alert-danger"
          when 'alert' then "alert alert-warning"


<% flash.each do |key, value| %>
   <div class="<%= flash_class(key) %> fade in">
      <a href="#" class="close" data-dismiss="alert">&times;</a>
      <%= value %>
<% end %>

Respondido 28 Abr '17, 20:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.