Rails - 设计 - 登录时出现错误消息?

时间:2011-01-08 20:33:04

标签: ruby-on-rails devise

如何让 f.error_messages 在这里工作,或者我应该使用闪光灯?
如果是这样,应该在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 工作正常。

6 个答案:

答案 0 :(得分:64)

尝试将这些放入您的布局中:

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

Devise中的登录操作设置了闪存消息,而不是模型错误。

答案 1 :(得分:31)

不可否认,有点hacky,但是我正在使用这个助手(app / helpers / devise_helper.rb)抓住闪光并使用那些如果设置然后默认为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

答案 2 :(得分:5)

尽管这篇文章已经很久了,但我想分享一个解决方案来帮助像我这样开始使用Devise时遇到麻烦的人。为了保持干净,我最后在我的application.html.erb文件中插入了这段代码:

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

  <%= yield %>
</body>

答案 3 :(得分:2)

这也应该

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

答案 4 :(得分:0)

创建帮助

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

    end

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

答案 5 :(得分:0)

简单。只需将下面的代码放在 views/devise/sessions/new.html.erb

<% if flash[:alert] %>
 <div class='alert alert-danger'><%= flash[:alert] %></div>
<% end %>

就是这样!