Skip to content

Instantly share code, notes, and snippets.

@lightscalar
Created December 26, 2009 19:47
Show Gist options
  • Save lightscalar/264024 to your computer and use it in GitHub Desktop.
Save lightscalar/264024 to your computer and use it in GitHub Desktop.
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end
accepts_nested_attributes_for :author,
:reject_if => proc { |attributes| attributes['name'].blank? }
OR TRY THIS:
class Person < ActiveRecord::Base
validates_presence_of :name
has_many :children, :class_name => 'Person'
accepts_nested_attributes_for :children, :allow_destroy => true
# can also be used on has_one etc.. associations
end
<% form_for @person do |person_form| %>
<%= person_form.label :name %>
<%= person_form.text_field :name %>
<% person_form.fields_for :children do |child_form| %>
<%= child_form.label :name %>
<%= child_form.text_field :name %>
<% unless child_form.object.new_record? %>
<%= child_form.check_box '_delete' %>
<%= child_form.label '_delete', 'Remove' %>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment