Unpermitted parameters: offices in a has_many :through association in Rails 5

195 Views Asked by At

I have a has_many through association. When I go to the Store form I need to save Owner data in the same form. But I keep getting Unpermitted parameters: :offices. I tried with inverse_of as well. I tried changing the models structures like trying to accept attributes for all models.

Office Model:

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

Owner Model:

class Owner < ApplicationRecord
  has_many :owner_offices
  has_many :offices, through: :owner_offices

  accepts_nested_attributes_for :owner_offices
end

Owner_Office Model:

class OwnerOffice < ApplicationRecord
  belongs_to :office
  belongs_to :owner

  accepts_nested_attributes_for :owner
end

Office Controller:

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

Office Form:

<%= 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 %>

I just placed most of the part where I'm calling the fields_for for owner. So I'm stuck, not sure what I'm missing at the moment and I've been checking other resources as well, implementing a different logic.

Also, I do not want to use cocoon because is important to me to learn how to implement from scratch.

Thanks in advance.

0

There are 0 best solutions below