【问题标题】:Error prohibited this user from being saved:错误禁止保存此用户:
【发布时间】:2014-07-14 03:11:06
【问题描述】:

我遇到了一个似乎无法解决的问题

3 个错误导致此用户无法保存:

  • 电子邮件不能为空
  • 密码确认与密码不匹配
  • 密码太短(最少 8 个字符)

我不知道如何摆脱3 error prohibited this user from being saved: 我只想显示项目符号中的错误。关于如何处理的任何想法?顺便说一句,我正在使用设计。

【问题讨论】:

  • 你使用的是什么版本的 Rails?
  • 请不要张贴文字截图。把它打出来,这样我们都可以阅读。
  • 帖子中有文字。主要问题在帖子@meagar中说明
  • 浏览 OPs 帖子,它看起来是最新的 @Paul

标签: html ruby-on-rails ruby forms devise


【解决方案1】:

我不知道如何摆脱 3 错误禁止此用户 被拯救了

我们覆盖了devise 帮助器,以最终控制我们希望显示的错误。 Here is a helpful resource 向您展示如何实现这一目标

--

助手

根据Buck Doyle 的回答和附加资源,您可能希望这样做:

#app/helpers/devise_helper.rb
module DeviseHelper
  def devise_error_messages!
     resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
  end
end

这将允许您更改您的 devise views 以包含 devise_error_messages! 方法:

#app/views/devise/registrations/new.html.erb
<%= devise_error_messages! %>

【讨论】:

  • @user302975:我不能确定,但​​我不认为把它放在 ApplicationHelper 中会真正覆盖 Devise 助手。这就是为什么我建议将它放在初始化程序中,并使用名称DeviseHelper,这肯定会覆盖现有方法。
  • 所以重命名为DeviseHelper? @BuckDoyle
  • 我没有DeviseHelper 只有application_helperhome_helper @BuckDoyle
  • 在此处添加 new 文件:config/initializers/devise_helper_override.rb 并输入我在答案中输入的 module DeviseHelper 代码。
  • 我收到&lt;li&gt;Email can&amp;#39;t be blank&lt;/li&gt;&lt;li&gt;Password can&amp;#39;t be blank&lt;/li&gt; Email 有什么想法吗?
【解决方案2】:

Devise 错误消息来自对devise_error_messages! 的调用,该调用位于devise/app/helpers/devise_helper.rb 的帮助器中。我通过运行bundle open devise 并查看视图发现了这一点。表单视图调用该辅助方法。

您可以将此代码放在初始化程序中以覆盖它:

module DeviseHelper
  def devise_error_messages!
    return "" if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join

    html = <<-HTML
    <div id="error_explanation">
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end
end

如果您与original 进行比较,您会发现我刚刚删除了构造一般错误消息的代码和包含它的h2

另一种选择是在 gem 之外使用 duplicate the views,然后您可以更精细地自定义它们。您将运行它,然后编辑生成的视图:

rails generate devise:views

【讨论】:

  • 我原来的只有module ApplicationHelperend。这正常吗?
  • 是的。 DeviseHelper 位于 Devise gem 中。您不会在应用程序结构中看到它。如果要查看 gem 中包含的文件,可以使用 bundle open devise 命令;你会在app/helpers 那里找到它。但我不建议编辑它,只需在其他地方覆盖它即可。
  • 我对另一个答案发表了评论;你是如何执行覆盖的?我认为你必须把它放在DeviseHelper 模块中,否则它不会真正覆盖原来的。
  • 不走运。我已经能够让它与 Rich Pecks 的帖子一起工作。但我得到&lt;li&gt;Email can&amp;#39;t be blank&lt;/li&gt;&lt;li&gt;Password can&amp;#39;t be blank&lt;/li&gt; Email
【解决方案3】:

老实说,最便宜的方法(虽然技术上不是删除标题)只是在你的 CSS 中添加以下内容:

#error_explanation h2 {
  display: none;
}

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2011-04-18
    • 2020-04-05
    相关资源
    最近更新 更多