见我的问题:Rails的accepts_nested_attributes_for + HABM返回数组与空字符串
class MedicalRecord < ActiveRecord::Base
has_many :evaluations, dependent: :destroy
accepts_nested_attributes_for :evaluations, allow_destroy: true, reject_if: :all_blank
end
class Evaluation < ActiveRecord::Base
belongs_to :medical_record
has_and_belongs_to_many :edemas
validates :description, presence: true
end
我的形式展示具有多个属性的选择字段。
<%= form_for @medical_record do |f| %>
<%= f.fields_for :evaluations do |e| %>
<%= e.text_field :description %>
<%= e.collection_select :edema_ids, Edema.all.order(:title), :id, :title,
{ }, multiple: true %>
<% end %>
<% end %>
当我没有选择任何'水肿',表单发送一个数组,其中包含一个空字符串。所以,reject_if
返回false
,我需要填写说明字段。在这种情况下,reject_if
应返回true
。
我该怎么办才能使其工作?
非常感谢
您可以包含为当前表单渲染的*当前*(处于“破碎”状态)的HTML吗? – jasonmklug