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.
@company
AND@company
=@incorporation.build_company
– argentum47