Skip to content

Instantly share code, notes, and snippets.

@brycesenz
Created May 23, 2014 15:03
Show Gist options
  • Save brycesenz/0ea951cbaecd30b791f2 to your computer and use it in GitHub Desktop.
Save brycesenz/0ea951cbaecd30b791f2 to your computer and use it in GitHub Desktop.
Forms for complexly nested models
class Person < ActiveRecord::Base
has_one :phone
has_many :dogs
attr_accessible :name
end
class Phone < ActiveRecord::Base
attr_accessible :phone_number
end
class Dog < ActiveRecord::Base
has_one :ball
attr_accessible :name
end
class Ball < ActiveRecord::Base
attr_accessible :color
end
{
:owner_name => "Tom",
:phone_number => "8885551234",
:dogs_attributes =>
{
"0" =>
{
:dog_name => "Buddy",
:ball_color => "Red",
},
"1" =>
{
:dog_name => "Max",
:ball_color => "Blue",
},
},
}
class DogOwnerForm < FreeForm::Form
form_models :person, :phone
validate_models
property :owner_name, :on => :person, :as => :name
property :phone_number, :on => :phone
has_many :dogs do
form_models :dog, :ball
validate_models
property :dog_name, :on => :dog, :as => :name
property :ball_color, :on => :ball, :as => :color
end
def dog_initializer
dog = person.dogs.build
{ :dog => dog, :ball => dog.build_ball }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment