Created
May 23, 2014 15:03
-
-
Save brycesenz/0ea951cbaecd30b791f2 to your computer and use it in GitHub Desktop.
Forms for complexly nested models
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 :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 |
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
{ | |
:owner_name => "Tom", | |
:phone_number => "8885551234", | |
:dogs_attributes => | |
{ | |
"0" => | |
{ | |
:dog_name => "Buddy", | |
:ball_color => "Red", | |
}, | |
"1" => | |
{ | |
:dog_name => "Max", | |
:ball_color => "Blue", | |
}, | |
}, | |
} |
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 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