These are my models:
class Parent < ActiveRecord::Base
has_one :child, :dependent => :destroy
end
class Child < ActiveRecord::Base
belongs_to :parent
accepts_nested_attributes_for :parent
end
My goal is update 1 parent attribute (email) when I'm creating the child (it means the end user is on the child form where the controller action is "new")
Hay que tener en cuenta que simpre cuando quiera crear el child, existe ya antes de antes un parent en la db.
My child controller:
def new
@child = Child.new
@child.parent = current_parent
end
def create
@child = Child.new(params[:child])
@child.parent = current_parent
respond_to do |format|
if @child.save
#.....
else
format.html { render :action => "new" }
end
end
end
Child form:
<% form_for @child, :html => {:multipart => true} do |f| %>
......
<% f.fields_for :parent do |p| %>
<%= p.label :email, t(:label_child_email), :req => true %>
<%= p.text_field :email, :class => "field" %>
<% end %>
<%end%>
When the users clicks on Save button they get:
Couldn't find Parent with ID=4147 for Child with ID=
And the parameters:
{"commit"=>"Save",
"child"=>{
...........
"parent_attributes"=>{"email"=>"[email protected]", "id"=>"4147"
},
..........
}
Do you know what's wrong ?
Thanks !