【问题标题】:Rails: form.error_messages does not wrap custom validated fields in a fieldWithErrors divRails:form.error_messages 不会将自定义验证字段包装在 fieldWithErrors div 中
【发布时间】:2009-11-05 16:51:24
【问题描述】:

我有一个使用低级别 validate_on_create 方法的模型,基于一个简单的条件,我在字段中添加了一条错误消息,例如

self.errors.add(:expiry_date, "is too soon") if self.expiry_date && self.expiry_date < Date.today + 1.week

在保存失败后呈现“新”视图时,error_messages_for 没有像我预期的那样拾取该字段。该错误显示在通常的错误列表中,表明验证工作正常。

有人知道这是为什么吗?我猜form.error_messages 没有查看对象上的所有错误?

添加了请求的代码:

<% form_for([:solicitor, @policy]) do |form| %>
  <%= form.error_messages :message => "See the list of reasons below to find out why not:", :header_message => "Sorry but we cannot yet create your policy  based on the information you've provided." %>
  <%= render :partial => 'form', :locals => {:form => form} %>
<% end %>

【问题讨论】:

  • 让我们看看你的form_for和form.error_messages查看代码。
  • 我刚刚添加了代码

标签: ruby-on-rails validation activerecord


【解决方案1】:

您的问题很可能是:

  1. 您没有通过 form_for 正确的选项。
  2. 您正在重定向回表单而不是呈现。
  3. 您没有使用 form_for 构建器(即 text_field_tag 而不是 f.text_field)

但是,没有发布您的完整控制器/视图,我基本上只是在猜测。

【讨论】:

  • 感谢您的回答,抱歉,我发布的内容不足以让您真正看到问题。
【解决方案2】:

问题是部分表单中的一个简单错字。我应该发布所有有问题的代码,因为我确信如果我做了 BJ Clark,否则其他人会很容易发现我犯的愚蠢错误

经验教训!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多