Skip to content

Instantly share code, notes, and snippets.

@avit
Created April 23, 2010 16:29
Show Gist options
  • Save avit/376763 to your computer and use it in GitHub Desktop.
Save avit/376763 to your computer and use it in GitHub Desktop.
# 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
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