Reputation: 2693
I have a situation where one of my nested fields is being passed to parameters but is not being inserted into the table.
My model Company
has_one :incorporation
. Incorporation has, right now anyway, one field, nested into the Company
form as follows
<%= simple_form_for @company, url: url_for(action: @caction, controller: 'incorporations'), html: {id:"incorporationform"}, remote: false, update: { success: "response", failure: "error"} do |company| %>
...
<%= company.simple_fields_for :incorporation do |f| %>
<div class="padded-fields">
<div class="form_subsection">
<%= f.input :trademark_search, as: :radio_buttons, label: 'Would you like us to do a trademark search and provide advice regarding any issues we identify in relation to the name you have selected?', input_html: { class: 'form-control radio radio-false' } %>
</div>
</div>
<% end %>
...
<% end %>
The new
and create
methods in the controller are as follows
def new
@user=current_user
@company = @user.companies.build
@incorporation = @company.build_incorporation
@action = "new"
@caction = "create"
@remote=false
end
def create
@snapshot="incorporation"
@company = current_user.companies.build(company_params)
@incorporation = @company.build_incorporation
if @company.save
current_user.companies << @company
if params[:final_submit]
redirect_to incorporations_index_path
else
redirect_to edit_incorporation_path(@incorporation), notice: "Successfuly saved incorporation info."
end
else
render 'new', notice: "Something went wrong; form unable to be saved."
end
end
The problem is that when I select one of the radio buttons in the simple fields, the log shows that the data is sent to the parameters:
Parameters: {"utf8"=>"✓",..., "company"=>{..."incorporation_attributes"=>{"trademark_search"=>"0"}},...}
But it is NOT being Inserted into the table. Instead, a new entry is inserted but only with the company_id
, id
, created_at
and updated_at
, all of which are perfectly accurate. There are no invalid parameter errors or anything like that.
My strong params look like this:
def company_params
params.require(:company).permit(:id, :name,...,incorporation_attributes: [:title, :trademark_search, :user_id, :employee_stock_options, :final_submit, :submit, :_destroy],...)
end
So somehow the parameters are not linking up with the table insertion (if that makes sense). What might be going on here?
Thanks for any ideas.
Upvotes: 2
Views: 620
Reputation: 454
Remove this line
@incorporation = @company.build_incorporation
from create method. This is again building the (Incorporation) record,. Nested forms will create the associated records if the parent object is created / updated,.(Company).
Upvotes: 1
Reputation: 76774
You only need to "build" the associated object if you're creating a new record (the build
record is blank):
def create
@company = current_user.companies.new company_params
@company.save #-> incorporation already set in params
Upvotes: 1
Reputation: 44685
Ok, there are some missing code, but i think issue is here:
@company = current_user.companies.build(company_params)
@incorporation = @company.build_incorporation
In first line you create company model together with incorporation model (which is present in company_params). Second line however overrides your incorporation model with a new one without any data. Since incorporation belongs_to company
, the initial one with all the data is discarded.
Solution: remove the second line entirely and all will work.
OTHER ERRORS:
You do not need this line:
current_user.companies << @company
@company
has already been build on current_user.companies
scope, meaning that it already belongs to current_user
. This will not cause any visible bugs, it's just waste of time for the server.
You are also missing id
within incorporation_attributes
- that means you will never be able to update or delete the data via nesting attributes (and you have _destroy field, so I assume you would like to do this at some point).
Upvotes: 2