具有多态关联的验证表单

时间:2010-02-10 22:49:43

标签: ruby-on-rails polymorphic-associations

class User<的ActiveRecord :: Base的       has_one:core,:as => :资源       validates_presence_of:name     端

class Core < ActiveRecord::Base
  belongs_to :resource, :polymorphic => true
  validates_presence_of     :email 
end

对于插入新用户,我使用如下表单:

<%= error_messages_for :user %>
<% form_for :user, :html => { :multipart => true, :id => "user_form", :class => "core_form" }, :url => "/users/create" do |user_form| -%>
<% user_form.fields_for :core_attributes, @user.core do |core_form| %>
   <%= core_form.text_field :name %>
   <%= user_form.text_field :email %>
<% end %>
  <% end %>

当我提交此表单时没有插入电子邮件和名称,rails只显示用户的错误。我也希望核心错误在同一个列表中 我该怎么办?

感谢

1 个答案:

答案 0 :(得分:1)

我想你可能想要validates_associated(* attr_names) http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002171

“验证关联的一个或多个对象本身是否都有效。适用于任何类型的关联。”