Repository navigation
Expand file tree
/
Copy pathapp.patch
More file actions
97 lines (87 loc) · 3.22 KB
/
Copy pathapp.patch
File metadata and controls
97 lines (87 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# TODO: remove this file from repo
From 3f49166f8f0c268dd7184404b292b8db8fa1b8b7 Mon Sep 17 00:00:00 2001
From: Phil McGachey <phil_mcgachey@harvard.edu>
Date: Sat, 1 Nov 2014 11:12:23 -0400
Subject: [PATCH] Fixes to the form generation and JSON serialization.
---
app/controllers/questions_controller.rb | 8 +++++---
app/helpers/questions_helper.rb | 4 ++--
app/models/question.rb | 3 ++-
db/migrate/20141101140632_add_fields_to_answer.rb | 6 ++++++
db/schema.rb | 4 +++-
5 files changed, 18 insertions(+), 7 deletions(-)
create mode 100644 db/migrate/20141101140632_add_fields_to_answer.rb
diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb
index a3e73ca..ab12bc1 100644
--- a/app/controllers/questions_controller.rb
+++ b/app/controllers/questions_controller.rb
@@ -7,12 +7,14 @@ class QuestionsController < ApplicationController
def new
@question = Question.new
- @answer = Answer.new
+ @question.answers.build
end
def create
- question = Question.new(params.require(:question).permit(:text, :answer))
- post_question(@question)
+ question = Question.new(params.require(:question)
+ .permit(:text,
+ answers_attributes:[:text]))
+ post_question(question)
redirect_to questions_path
end
end
diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb
index 49300c0..cbfb042 100644
--- a/app/helpers/questions_helper.rb
+++ b/app/helpers/questions_helper.rb
@@ -8,8 +8,8 @@ module QuestionsHelper
end
def post_question question
- json = question.to_json
- response = RestClient.post('http://localhost:3000/questions', :data => json, :content_type => :json , :accept => :json)
+ json = question.to_json(:include => :answers)
+ response = RestClient.post('http://localhost:3000/questions', json, :content_type => :json , :accept => :json)
print "THIS IS THE #{response}"
end
end
diff --git a/app/models/question.rb b/app/models/question.rb
index 78b4ead..94140f5 100644
--- a/app/models/question.rb
+++ b/app/models/question.rb
@@ -1,3 +1,4 @@
class Question < ActiveRecord::Base
- has_one :answers
+ has_many :answers
+ accepts_nested_attributes_for :answers
end
diff --git a/db/migrate/20141101140632_add_fields_to_answer.rb b/db/migrate/20141101140632_add_fields_to_answer.rb
new file mode 100644
index 0000000..d35c609
--- /dev/null
+++ b/db/migrate/20141101140632_add_fields_to_answer.rb
@@ -0,0 +1,6 @@
+class AddFieldsToAnswer < ActiveRecord::Migration
+ def change
+ add_column :answers, :question_id, :int
+ add_column :answers, :text, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 352a799..9776cf1 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,11 +11,13 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20141030231443) do
+ActiveRecord::Schema.define(version: 20141101140632) do
create_table "answers", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "question_id"
+ t.string "text"
end
create_table "questions", force: true do |t|
--
1.9.3 (Apple Git-50)