I have
class Deal < ApplicationRecord
belongs_to :company, dependent: :destroy
accepts_nested_attributes_for :company
class DealsController < ApplicationController
def update
respond_to do |format|
if @deal.update(deal_params)
format.html { redirect_to dashboard_path, notice: 'Deal was successfully updated.' }
...
def deal_params
params.require(:deal).permit :name, ...
company_attributes: [:name, ...]
When it submits the form, I see
Parameters: {"utf8"=>"√", "deal"=>{"name"=>"Office building", ...
"company_attributes"=>{"name"=>"...", "id"=>"25"}}, "commit"=>"Update Deal", "id"=>"1"}
Unpermitted parameter: id
But it always creates a new Company
model instead of updating the existing associated model. I don't know why it's creating a new model. I don't include :id
in company_attributes
array because I'm afraid someone could use it to update something that doesn't belong to them.
Why is Rails always creating a new Company
model instead of updating the existing Company
model?
This question is similar but doesn't answer my question.
Rails 5.0.2
HTML requested:
<div class="form-group">
<label for="deal_company_attributes_name">Name</label>
<input class="form-control" type="text" value="West Side Offices LLC" name="deal[company_attributes][name]" id="deal_company_attributes_name">
</div>
...
<input type="hidden" value="26" name="deal[company_attributes][id]" id="deal_company_attributes_id">
<div class="actions">
<input type="submit" name="commit" value="Update Deal" class="btn btn-primary" data-disable-with="Update Deal">
</div>