我有这个问题。我需要在Rails中以相同的形式验证两个模型的属性。一个是另一个的父母。Rails中嵌套模型的表单验证
的形式是这样的:
<%= semantic_form_for @professional do |pro| %>
<%= pro.inputs :id => "information" do %>
<%= pro.input :name, label: t("Artistic Name") %>
<%= pro.semantic_fields_for @user do |user| %>
<%= user.inputs :id => "register" do %>
<%= user.input :email, :placeholder=>"[email protected]" %>
<%= user.input :password, label: t('Password') %>
<%end%>
<% end %>
<% end %>
<% end %>
我使用的模型是这样的:
用户:
class User < ActiveRecord::Base
belongs_to :role, polymorphic: true
validates :email, :password, presence: true
end
专业人员:
class Professional < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy
accepts_nested_attributes_for :user
validates :date_birthday, :gender, :height, :name, :description, :Weight, :address, :languages,:services, :category, :phonenumber, :fullname, :hair_color, :age, :orientation, presence: true
end
所以,问题是什么?
当我点击提交按钮时,专业属性被标记,但不是用户属性。
像这样:
的字段标为红色属于专业模特,但场电子邮件地址和密码属于用户模型,红色都没有标明时,它应该是因为他们是空。
我该怎么办?我需要用户的警告信息是属性太
谢谢你的进步。
你有关联设置是否正确? –
@RichPeck我编辑了两个模型之间的关联模型代码 –