У меня есть has_many через ассоциацию.Когда я захожу в форму Store , мне нужно сохранить данные Owner в той же форме.Но я продолжаю получать Unpermitted parameters: :offices
.Я попытался с inverse_of также.Я попытался изменить структуры моделей, например попытаться принять атрибуты для всех моделей.
Модель офиса:
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
Контроллер офиса:
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 %>
Я просто поместил большую часть части, где я звоню, fields_for
для владелец .Так что я застрял, не уверен, что мне не хватает в данный момент, и я также проверял другие ресурсы, реализуя другую логику.
Кроме того, я не хочу использовать кокон, потому что это важномне научиться реализовывать с нуля.
Заранее спасибо.