1
votes

I'm having a problem with saving the contents of a nested field. I have two models Incorporation and Company. They relate as follows:

class Company < ActiveRecord::Base
  belongs_to :incorporation
end

class Incorporation < ActiveRecord::Base
  has_one :company

  accepts_nested_attributes_for :company
end

My aim is to create a new Company and Incorporation entry in the same form, using both the incorporations controller and view.

(Problem) However, each time I attempt to submit the form, the Incorporation entry goes through but the company entry is held up with the Unpermitted parameters error:

Started POST "/incorporations" for 127.0.0.1 at 2014-12-15 22:40:59 -0700
Processing by IncorporationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lCj/ZtNNE/9l/UAlYcnA8EAe8vmMN010toS4t5e+ZkA=", "incorporation"=>{"title"=>"test", "company"=>{"name"=>"test"}}, "button"=>""}
Unpermitted parameters: company
Completed 500 Internal Server Error in 4ms

This is particularly surprising as I beleive I've set my strong params correctly. Below is my controller.

class IncorporationsController < ApplicationController
  def index
  end

  def show
  end

  def new
@incorporation = Incorporation.new
@company = Company.new
  end

  def create
@incorporation = Incorporation.new(incorporation_params)

if @incorporation.save
  redirect_to @incorporation, notice: "Successfuly saved incorporation info."
else
  render 'new'
end
  end

  def edit
  end

  def show
  end

  private

  def incorporation_params
params.require(:incorporation).permit(:title, company_attributes: [:name, :state_corp, :street, :city, :state, :zip, :outstanding_common_stock, :fiscal_year_end_month, :fiscal_year_end_day])
  end
end

The form partial I'm using is as follows:

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for @company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>

<% end =%>

Any ideas would be appreciated.

2
:company not @company AND @company = @incorporation.build_companyargentum47
@argentum47 Hey dude, thanks a lot! Feel free to write up your comment as an answer so I can give you credit.neanderslob

2 Answers

1
votes

The usual way to do the above would be

Controller

  def new
  @incorporation = Incorporation.new
  @company = @incorporation.build_company

and in your view

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for :company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>
<% end %>

The rest is fine.

0
votes

Cross check few things in your code base.

1) You should have incorporation_id in company table. If you don't have then add one.

2) Try change following code in new action:

def new
  @incorporation = Incorporation.new
  @incorporation.company = Company.new
  #Or you can simply build company also like below
  #@incorporation.company.build
end

Try change following in view:

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for :company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>

<% end =%>