I have a parent model that has one child model with nested attributes. I have a single form that updates both parent and child.
Here are my models:
class Parent < ActiveRecord::Base
has_one :child
accepts_nested_attributes_for :child
end
class Child < ActiveRecord::Base
belongs_to :parent
end
Form view:
<%= form_for @parent, do |f| %>
<%= f.text_field :parent_name %>
<%= f.fields_for @parent.child do |c| %>
<%= c.text_field :child_name %>
<% end %>
<%= f.submit "Save" %>
<% end %>
Parent controller:
class ParentsController < ApplicationController
def update
@parent = Parent.find(params[:id])
@parent.update(params.require(:parent).permit(:parent_name, child_attributes: [:child_name]))
redirect_to @parent
end
end
When I save the form, the parent updates but the child doesn't. What am I doing wrong?