我通过关联拥有has_many。当我转到商店表单时,我需要以相同的形式保存所有者数据。但是我不断得到Unpermitted parameters: :offices
。我也尝试过inverse_of。我试图更改模型结构,例如尝试接受所有模型的属性。
Office模型:
class Office < ApplicationRecord
has_many :owner_offices, :dependent => :destroy
has_many :owners, through: :owner_offices
accepts_nested_attributes_for :owner_offices
#accepts_nested_attributes_for :offices
end
所有者模型:
class Owner < ApplicationRecord
has_many :owner_offices
has_many :offices, through: :owner_offices
accepts_nested_attributes_for :owner_offices
end
Owner_Office模型:
class OwnerOffice < ApplicationRecord
belongs_to :office
belongs_to :owner
accepts_nested_attributes_for :owner
end
Office控制器:
def new
@office = Office.new
@office.owners.build
end
def office_params
params.require(:office).permit(:office_name, :office_slug, :office_email, :phone, :office_type, :status, :mf_member, :comment,
:owners_attributes => [:office_id, :owner_id, :first_name, :last_name, :owner_email])
end
办公室表格:
<%= form_with(model: office, local: true, html: {class: "form-office"}) do |form| %>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span><%= form.label :office_email %></span>
<%= form.text_field :office_email, class: 'form-control' %>
</div>
</div>
</div>
<hr>
<h3>Owner Information</h3>
<hr>
<%= form.fields_for :owners do |owner_form| %>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span><%= owner_form.label :first_name %></span>
<%= owner_form.text_field :first_name, class: 'form-control' %>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span><%= owner_form.label :last_name %></span>
<%= owner_form.text_field :last_name, class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span><%= owner_form.label :owner_email %></span>
<%= owner_form.text_field :username, class: 'form-control' %>
</div>
</div>
</div>
<% end %>
<% end %>
我只是将大部分内容放在我称为{strong>所有者的fields_for
上。因此,我很困惑,不确定当前缺少什么,我也一直在检查其他资源,实现了不同的逻辑。
此外,我也不想使用茧,因为对于我来说,从头开始学习实现至关重要。
谢谢。