【问题标题】:Rails: Do not show "<model> is not valid" on errors.full_messagesRails:不要在 errors.full_messages 上显示“<model> is not valid”
【发布时间】:2012-09-13 16:58:21
【问题描述】:

我只想显示属性的错误,而不是某个对象在视图中无效。我该怎么办?

我目前使用这个:

<% if !@ozb_konto.errors.nil? && @ozb_konto.errors.any? %>
<div class="alert alert-error" id="error_explanation">
  <h3>Following errors occured:</h3>    
  <ul>
    <% @ozb_konto.errors.full_messages.each do |error| %>
      <li><%= error %></li>
    <% end %>
      </ul>
</div>
<% end %>

这显示了所有类型的错误,这是正确的。但它也显示了我想要抑制的&lt;model&gt; is not valid 错误。

我该如何处理?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails view error-suppression


    【解决方案1】:

    作为full_messages的方法是:

    def full_messages
      map { |attribute, message| full_message(attribute, message) }
    end
    

    您可以将错误转换为哈希并根据需要输出:

    @ozb_konto.errors.to_hash # => {:email=>["can't be blank"], :password=>["can't be blank"], :name=>["can't be blank"]} e.g
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 2018-03-29
      • 2017-03-03
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多