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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4635986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:02:50  来源:igfitidea点击:

Rails - Devise - Error messages when signing in?

ruby-on-railsdevise

提问by Frexuz

How can I make f.error_messageswork here, or should I use flashes?
If so, what should override in the sessions_controller?

我怎样才能让f.error_messages在这里工作,或者我应该使用闪光灯?
如果是这样,应该在 session_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_messagesfor Creating an account works totally fine.

附注。用于创建帐户的f.error_messages完全正常。

采纳答案by typeoneerror

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.

诚然,有点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

回答by ffoeg

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.

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

回答by Carl Edwards

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.erbfile:

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

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

  <%= yield %>
</body>

回答by Sachin

This shall also do

这也应该做

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

回答by gilcierweb

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