Rails - 设计 - 登录时出现错误消息? [英] Rails - Devise - Error messages when signing in?

查看:23
本文介绍了Rails - 设计 - 登录时出现错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 f.error_messages 在这里工作,还是应该使用闪光灯?
如果是这样,应该在 session_controller 中覆盖什么?

创建一个帐户

<% form_for resource_name, resource, :url =>注册路径(资源名称)做 |f|%><%= f.error_messages %><p><%= f.label :email %><br/><%= f.text_field :email, :class =>:大%></p><p><%= f.label :password %><br/><%= f.password_field :password, :class =>:大%></p><p><%= f.label :password_confirmation %><br/><%= f.password_field :password_confirmation, :class =>:大%></p><p><%= f.submit "Create", :class =>:提交%></p><%结束%>

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

解决方案

诚然,有点 hacky,但我正在使用这个助手 (app/helpers/devise_helper.rb) 来抓取 Flash 并使用那些如果设置然后默认为resource.errors.

module DeviseHelperdef devise_error_messages!flash_alerts = []error_key = 'errors.messages.not_saved'如果!flash.empty?flash_alerts.push(flash[:error]) 如果 flash[:error]flash_alerts.push(flash[:alert]) 如果 flash[:alert]flash_alerts.push(flash[:notice]) 如果 flash[:notice]error_key = 'devise.failure.invalid'结尾如果resource.errors.empty 返回""?&&flash_alerts.empty?错误 = 资源.错误.空??flash_alerts:resource.errors.full_messages消息 = 错误.map { |msg|content_tag(:li, msg) }.join句子 = I18n.t(error_key, :count => errors.count,:资源 =>resource.class.model_name.human.downcase)html = <<-HTML<div id="error_explanation"><h2>#{sentence}</h2><ul>#{messages}</ul>

HTMLhtml.html_safe结尾结尾

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.

解决方案

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

这篇关于Rails - 设计 - 登录时出现错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆