使用accepts_nested_attributes_for的自定义验证消息

时间:2010-12-28 19:25:15

标签: ruby-on-rails ruby validation

我有一个User模型has_one :contact_detailaccepts_nested_attributes_for :contact_detail。我正在尝试在en.yml文件中进行自定义字段验证:

en:
  activerecord:
    attributes:
      user:
        email: "E-Mail Address"
      contact_detail:
        address1: "Address Line 1"

这是我认为的表格:

 <h2>User Account Details</h2>

 <ul>
  <li><%= f.label :username, "Username" %> <%= f.text_field :username, :class => "required" %></li>
  <li><%= f.label :email, "Email Address" %> <%= f.text_field :email, :class => "required" %></li>
 </ul>

 <h2>Address Details</h2>

 <%= f.fields_for :contact_detail, @staff_member.contact_detail do |a| %>
 <ul>
  <li><%= a.label :address1, "Address Line 1" %> <%= a.text_field :address1, :class => "required" %></li>
 </ul>
 <% end %>

但是,我得到的错误消息如下。它似乎没有考虑嵌套的形式...

Contact detail address1 can't be blank
Username can't be blank

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

YML文件是嵌套的 - 考虑到这一点似乎可以解决问题。

en:
  activerecord:
    attributes:
      user:
        email: "E-Mail Address"
        contact_detail:
          address1: "Address Line 1"