我是否遗漏了使用accept_nested_attributes_for和mongoid的事情?我正在尝试使用嵌套表单创建具有has_many关系的父对象和子对象。保存子对象,但parent_id为null。
def new
@company = Company.new
3.times { @company.invitations.build }
end
private
def company_params
params[:company].permit(:name, :slug, :invitations_attributes => [:recipient_email, :company_id])
end
邀请模式
belongs_to :company
公司模式
field :_id, type: String, default: ->{ slug }
field :name
field :slug
has_many :invitations
accepts_nested_attributes_for :invitations, :reject_if => lambda { |a| a[:recipient_email].blank? }
嵌套表格
<%= form_for @company do |f|%>
<%= f.label :name%>
<%= f.text_field :name%>
<%= f.label :slug%>
<%= f.text_field :slug%>
<%= f.label :invitations, "Invite Emails"%>
<%= f.fields_for :invitations do |i| %>
<%= i.email_field :recipient_email %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
来自日志
{&#34;插入&#34; = GT;&#34;邀请&#34 ;, &#34;文件&#34; = GT; [{&#34; _id&#34; = GT; BSON ::的ObjectId(&#39; 587e88158a6818eb8f4bb64b&#39), &#34; recipient_email&#34; =&gt;&#34; some-email@gmail.com",&#34; company_id&#34; =&gt; nil, &#34; updated_at&#34; =&gt; 2017-01-17 21:09:42 UTC,&#34; created_at&#34; =&gt; 2017-01-17 21:09:42 UTC}