Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Devise - Error messages when signing in?

How can I make 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 %>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email, :class => :big %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password, :class => :big %>
  </p>
  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, :class => :big %>
  </p>

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

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

like image 903
Frexuz Avatar asked Jan 08 '11 20:01

Frexuz


4 Answers

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.

like image 51
ffoeg Avatar answered Oct 22 '22 20:10

ffoeg


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'
    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
like image 33
typeoneerror Avatar answered Oct 22 '22 20:10

typeoneerror


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 file:

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

  <%= yield %>
</body>
like image 6
Carl Edwards Avatar answered Oct 22 '22 20:10

Carl Edwards


This shall also do

<% flash.each do |name, msg| %>
  <%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>
like image 3
Sachin Avatar answered Oct 22 '22 20:10

Sachin