Skip to content

Instantly share code, notes, and snippets.

@danvine
Created February 2, 2012 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvine/1723614 to your computer and use it in GitHub Desktop.
Save danvine/1723614 to your computer and use it in GitHub Desktop.
diff --git a/app/controllers/parents_controller.rb b/app/controllers/parents_controller.rb
index 4f6831f..ce6c226 100644
--- a/app/controllers/parents_controller.rb
+++ b/app/controllers/parents_controller.rb
@@ -25,6 +25,7 @@ class ParentsController < ApplicationController
# GET /parents/new.json
def new
@parent = Parent.new
+ @parent.children.build
respond_to do |format|
format.html # new.html.erb
diff --git a/app/models/child.rb b/app/models/child.rb
index 0a4ff5d..d51105b 100644
--- a/app/models/child.rb
+++ b/app/models/child.rb
@@ -1,2 +1,3 @@
class Child < ActiveRecord::Base
+ belongs_to :parent
end
diff --git a/app/models/parent.rb b/app/models/parent.rb
index 5771dca..3339d73 100644
--- a/app/models/parent.rb
+++ b/app/models/parent.rb
@@ -1,2 +1,6 @@
class Parent < ActiveRecord::Base
+ attr_accessible :name, :age, :children_attributes
+ has_many :children
+
+ accepts_nested_attributes_for :children, :allow_destroy => true
end
diff --git a/app/views/parents/_form.html.erb b/app/views/parents/_form.html.erb
index d4522a8..cdc5480 100644
--- a/app/views/parents/_form.html.erb
+++ b/app/views/parents/_form.html.erb
@@ -20,6 +20,18 @@
<%= f.number_field :age %>
</div>
+ <%= f.fields_for :children do |sub| %>
+ <hr />
+ <%= sub.label :toy, "toy" %><br />
+ <%= sub.text_field :toy %><br />
+
+ <%= sub.label :hair, "hair" %><br />
+ <%= sub.text_field :hair %><br />
+
+ <%= sub.label :eyes, "eyes" %><br />
+ <%= sub.text_field :eyes %><br />
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
zippy:wtf danvine$ git add command_history
zippy:wtf danvine$ git commit -m"c"
[master fd25e9b] c
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 command_history
zippy:wtf danvine$ git diff | fgrep rake
+rake db:migrate
zippy:wtf danvine$ git diff
diff --git a/app/controllers/parents_controller.rb b/app/controllers/parents_controller.rb
index 4f6831f..ce6c226 100644
--- a/app/controllers/parents_controller.rb
+++ b/app/controllers/parents_controller.rb
@@ -25,6 +25,7 @@ class ParentsController < ApplicationController
# GET /parents/new.json
def new
@parent = Parent.new
+ @parent.children.build
respond_to do |format|
format.html # new.html.erb
diff --git a/app/models/child.rb b/app/models/child.rb
index 0a4ff5d..d51105b 100644
--- a/app/models/child.rb
+++ b/app/models/child.rb
@@ -1,2 +1,3 @@
class Child < ActiveRecord::Base
+ belongs_to :parent
end
diff --git a/app/models/parent.rb b/app/models/parent.rb
index 5771dca..3339d73 100644
--- a/app/models/parent.rb
+++ b/app/models/parent.rb
@@ -1,2 +1,6 @@
class Parent < ActiveRecord::Base
+ attr_accessible :name, :age, :children_attributes
+ has_many :children
+
+ accepts_nested_attributes_for :children, :allow_destroy => true
end
diff --git a/app/views/parents/_form.html.erb b/app/views/parents/_form.html.erb
index d4522a8..cdc5480 100644
--- a/app/views/parents/_form.html.erb
+++ b/app/views/parents/_form.html.erb
@@ -20,6 +20,18 @@
<%= f.number_field :age %>
</div>
+ <%= f.fields_for :children do |sub| %>
+ <hr />
+ <%= sub.label :toy, "toy" %><br />
+ <%= sub.text_field :toy %><br />
+
+ <%= sub.label :hair, "hair" %><br />
+ <%= sub.text_field :hair %><br />
+
+ <%= sub.label :eyes, "eyes" %><br />
+ <%= sub.text_field :eyes %><br />
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
diff --git a/command_history b/command_history
index e69de29..96e8559 100644
--- a/command_history
+++ b/command_history
@@ -0,0 +1,5 @@
+mkdir parents_children
+rails new parents_children
+rails g scaffold Parent name:string age:integer
+rails g scaffold Children toy:string hair:string eyes:string parent_id:integer
+rake db:migrate
\ No newline at end of file
zippy:wtf danvine$ git diff
diff --git a/app/controllers/parents_controller.rb b/app/controllers/parents_controller.rb
index 4f6831f..ce6c226 100644
--- a/app/controllers/parents_controller.rb
+++ b/app/controllers/parents_controller.rb
@@ -25,6 +25,7 @@ class ParentsController < ApplicationController
# GET /parents/new.json
def new
@parent = Parent.new
+ @parent.children.build
respond_to do |format|
format.html # new.html.erb
diff --git a/app/models/child.rb b/app/models/child.rb
index 0a4ff5d..d51105b 100644
--- a/app/models/child.rb
+++ b/app/models/child.rb
@@ -1,2 +1,3 @@
class Child < ActiveRecord::Base
+ belongs_to :parent
end
diff --git a/app/models/parent.rb b/app/models/parent.rb
index 5771dca..3339d73 100644
--- a/app/models/parent.rb
+++ b/app/models/parent.rb
@@ -1,2 +1,6 @@
class Parent < ActiveRecord::Base
+ attr_accessible :name, :age, :children_attributes
+ has_many :children
+
+ accepts_nested_attributes_for :children, :allow_destroy => true
end
diff --git a/app/views/parents/_form.html.erb b/app/views/parents/_form.html.erb
index d4522a8..cdc5480 100644
--- a/app/views/parents/_form.html.erb
+++ b/app/views/parents/_form.html.erb
@@ -20,6 +20,18 @@
<%= f.number_field :age %>
</div>
+ <%= f.fields_for :children do |sub| %>
+ <hr />
+ <%= sub.label :toy, "toy" %><br />
+ <%= sub.text_field :toy %><br />
+
+ <%= sub.label :hair, "hair" %><br />
+ <%= sub.text_field :hair %><br />
+
+ <%= sub.label :eyes, "eyes" %><br />
+ <%= sub.text_field :eyes %><br />
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
diff --git a/command_history b/command_history
index e69de29..7f85a96 100644
--- a/command_history
+++ b/command_history
@@ -0,0 +1,5 @@
+mkdir parents_children
+rails new parents_children
+rails g scaffold Parent name:string age:integer
+rails g scaffold Children toy:string hair:string eyes:string parent_id:integer
+rake db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment