Created
April 23, 2010 16:29
-
-
Save avit/376763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use an instance for the top-level form_for | |
form_for @parent do |f| | |
# Always use a symbol to access the nested association from f.object | |
f.fields_for :child do |fc| | |
# generates: params[:parent][:child_attributes][:field] | |
end | |
# Optionally, provide an instance as a *second* argument | |
f.fields_for :relative, (@other_parent or Person.new) do |fr| | |
# generates: params[:parent][:relative_attributes][:field] | |
end | |
# Not like this: | |
f.fields_for @child do |fc| | |
# generates: params[:parent][:child][:field] | |
# error on nested attribute assignment in controller: | |
# Child(#2172589160) expected, got HashWithIndifferentAccess(#2156610500) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
has_one :child | |
has_one :relative | |
accepts_nested_attributes_for :child, :relative | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment