ruby on rails - Nested attributes not being inserted into table -
i have situation 1 of nested fields being passed parameters not being inserted table.
my model company
has_one :incorporation
. incorporation has, right anyway, 1 field, nested company
form follows
<%= simple_form_for @company, url: url_for(action: @caction, controller: 'incorporations'), html: {id:"incorporationform"}, remote: false, update: { success: "response", failure: "error"} |company| %> ... <%= company.simple_fields_for :incorporation |f| %> <div class="padded-fields"> <div class="form_subsection"> <%= f.input :trademark_search, as: :radio_buttons, label: 'would trademark search , provide advice regarding issues identify in relation name have selected?', input_html: { class: 'form-control radio radio-false' } %> </div> </div> <% end %> ... <% end %>
the new
, create
methods in controller 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 saved." end end
the problem when select 1 of radio buttons in simple fields, log shows data sent parameters:
parameters: {"utf8"=>"✓",..., "company"=>{..."incorporation_attributes"=>{"trademark_search"=>"0"}},...}
but not being inserted table. instead, new entry inserted company_id
, id
, created_at
, updated_at
, of accurate. there no invalid parameter errors or that.
my strong params 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 parameters not linking table insertion (if makes sense). might going on here?
thanks ideas.
ok, there missing code, think issue here:
@company = current_user.companies.build(company_params) @incorporation = @company.build_incorporation
in first line create company model incorporation model (which present in company_params). second line overrides incorporation model new 1 without data. since incorporation belongs_to company
, initial 1 data discarded.
solution: remove second line entirely , work.
other errors:
you not need line:
current_user.companies << @company
@company
has been build on current_user.companies
scope, meaning belongs current_user
. not cause visible bugs, it's waste of time server.
you missing id
within incorporation_attributes
- means never able update or delete data via nesting attributes (and have _destroy field, assume @ point).
Comments
Post a Comment