質問編集履歴

3

comment.rb、comments_controller.rb 追加

2020/01/01 14:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  ```Ruby
16
16
 
17
+ (post_controller.rb)
18
+
17
19
  def show
18
20
 
19
21
  @post = Post.find_by(id: params[:id])
@@ -126,6 +128,8 @@
126
128
 
127
129
  ```Ruby
128
130
 
131
+ (schema.rb)
132
+
129
133
  class CreateComments < ActiveRecord::Migration[5.2]
130
134
 
131
135
  def change
@@ -146,6 +150,78 @@
146
150
 
147
151
  end
148
152
 
149
-
150
-
151
- ```
153
+ ```
154
+
155
+
156
+
157
+ ```Ruby
158
+
159
+ (comment.rb)
160
+
161
+ class Comment < ApplicationRecord
162
+
163
+ belongs_to :posts
164
+
165
+ validates :content, presence: true
166
+
167
+ end
168
+
169
+ ```
170
+
171
+
172
+
173
+ ```Ruby
174
+
175
+ (comments_controller.rb)
176
+
177
+ class CommentsController < ApplicationController
178
+
179
+ private
180
+
181
+ def content_params
182
+
183
+ params.require(:comment).permit(:content)
184
+
185
+ end
186
+
187
+
188
+
189
+ def create
190
+
191
+ post = Post.find(params[:post_id])
192
+
193
+ @comment = post.comments.build(comment_params)
194
+
195
+ if @comment.save
196
+
197
+ flash[:success] = "コメントしました"
198
+
199
+ redirect_back(fallback_location: image_url(post.id))
200
+
201
+ else
202
+
203
+ flash[:danger] = "コメントできません"
204
+
205
+ redirect_back(fallback_location: image_url(post.id))
206
+
207
+ end
208
+
209
+ end
210
+
211
+
212
+
213
+ def destroy
214
+
215
+ post = Post.find(params[:post_id])
216
+
217
+ @comment = post.comments.find(params[:id])
218
+
219
+ @comment.destroy
220
+
221
+ redirect_back(fallback_location: image_path(post)
222
+
223
+ end
224
+
225
+ end
226
+
227
+ ```

2

schema.rb commentテーブル変更

2020/01/01 14:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  ```Ruby
50
50
 
51
- (schema.rb)
51
+ (schema.rb) (変更後)
52
52
 
53
53
  ActiveRecord::Schema.define(version: 2020_01_01_011953) do
54
54
 
@@ -58,13 +58,13 @@
58
58
 
59
59
  t.string "content"
60
60
 
61
- t.integer "posts_id"
61
+ t.integer "post_id"
62
62
 
63
63
  t.datetime "created_at", null: false
64
64
 
65
65
  t.datetime "updated_at", null: false
66
66
 
67
- t.index ["posts_id"], name: "index_comments_on_posts_id"
67
+ t.index ["post_id"], name: "index_comments_on_post_id"
68
68
 
69
69
  end
70
70
 

1

schema.rbとmigrationファイルを追加

2020/01/01 14:04

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -39,3 +39,113 @@
39
39
  どなたか直し方を教えていただけないでしょうか。
40
40
 
41
41
  よろしくお願いします。
42
+
43
+
44
+
45
+ 以下、追記。
46
+
47
+
48
+
49
+ ```Ruby
50
+
51
+ (schema.rb)
52
+
53
+ ActiveRecord::Schema.define(version: 2020_01_01_011953) do
54
+
55
+
56
+
57
+ create_table "comments", force: :cascade do |t|
58
+
59
+ t.string "content"
60
+
61
+ t.integer "posts_id"
62
+
63
+ t.datetime "created_at", null: false
64
+
65
+ t.datetime "updated_at", null: false
66
+
67
+ t.index ["posts_id"], name: "index_comments_on_posts_id"
68
+
69
+ end
70
+
71
+
72
+
73
+ create_table "likes", force: :cascade do |t|
74
+
75
+ t.integer "user_id"
76
+
77
+ t.integer "post_id"
78
+
79
+ t.datetime "created_at", null: false
80
+
81
+ t.datetime "updated_at", null: false
82
+
83
+ end
84
+
85
+
86
+
87
+ create_table "posts", force: :cascade do |t|
88
+
89
+ t.text "content"
90
+
91
+ t.datetime "created_at", null: false
92
+
93
+ t.datetime "updated_at", null: false
94
+
95
+ t.integer "user_id"
96
+
97
+ t.string "post_image_name"
98
+
99
+ end
100
+
101
+
102
+
103
+ create_table "users", force: :cascade do |t|
104
+
105
+ t.string "name"
106
+
107
+ t.string "email"
108
+
109
+ t.datetime "created_at", null: false
110
+
111
+ t.datetime "updated_at", null: false
112
+
113
+ t.string "image_name"
114
+
115
+ t.string "password_digest"
116
+
117
+ end
118
+
119
+
120
+
121
+ end
122
+
123
+ ```
124
+
125
+
126
+
127
+ ```Ruby
128
+
129
+ class CreateComments < ActiveRecord::Migration[5.2]
130
+
131
+ def change
132
+
133
+ create_table :comments do |t|
134
+
135
+ t.string :content
136
+
137
+ t.references :posts, foreign_key: true
138
+
139
+
140
+
141
+ t.timestamps
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+ ```