0

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.

  • What is the value of `params`, once the request hits your controller? – XML Slayer Sep 27 '18 at 18:33
  • @XMLSlayer I think you mean this request? (some fields are empty since is just a test.) `Parameters: {"utf8"=>"✓", "office"=>{"office_name"=>"Office tx-21", "office_slug"=>"office-tx-21", "office_email"=>"officetx-21@gmail.com", "phone"=>"", "owners"=>{"first_name"=>"Michael", "last_name"=>"Larson", "username"=>"michael@officetx-21.com"}, "commit"=>"Create Office"} Unpermitted parameters: :offices (0.2ms) BEGIN ↳ app/controllers/offices_controller.rb:33` – Michael Russell Sep 27 '18 at 18:48
  • Are you render this form in the `new` action? Could you post full view? – Roman Kiselenko Sep 27 '18 at 18:58
  • You have `has_many :offices, through: :owner_offices` in `Office` model? Shouldn't it be `has_many :owners, through: :owner_offices`? – Pavan Sep 27 '18 at 18:58
  • @Зелёный I just placed the form where is calling the nested attributes. The form is the default from a rails scaffold I did a scaffold for Office. No modifications besides including the `fields_for`. I mean, more fields like "name", "last_name" and the button. – Michael Russell Sep 27 '18 at 19:44
  • @Pavan you were right. Was a typo writing the question. In my code I checked is is ok. I edited the question as well. Thank you – Michael Russell Sep 27 '18 at 19:46
  • Are you sure that the error is `Unpermitted parameters: :offices`? – Pavan Sep 27 '18 at 19:49
  • Yes @Pavan, I pasted the error above in a comment: `Unpermitted parameters: :offices` – Michael Russell Sep 27 '18 at 21:33
  • Also @Pavan I was trying a different way to save, like this `@owner = @office.owners.build` but still not working – Michael Russell Sep 27 '18 at 21:40

0 Answers0