質問編集履歴
2
しんてんがあったのでついきしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -305,3 +305,21 @@
|
|
305
305
|
ruby 2.6.5
|
306
306
|
|
307
307
|
docker 20.10.0
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
#追記
|
312
|
+
|
313
|
+
posts/index.html.erbにの以下のコード
|
314
|
+
|
315
|
+
`<% @posts.comment.each do |comment|%>`
|
316
|
+
|
317
|
+
を載せて見た後に、postのほうのmodelに以下のコードを追加したら
|
318
|
+
|
319
|
+
`belongs_to :comments`
|
320
|
+
|
321
|
+
エラーになってしまいました。
|
322
|
+
|
323
|
+
スクショを貼っておきます
|
324
|
+
|
325
|
+
![イメージ説明](e324de9f96b1c148df28b43172970c9c.png)
|
1
controllerを追記しました。それと、dockerを使っているので、投稿したときのログも載せておきます
test
CHANGED
File without changes
|
test
CHANGED
@@ -126,6 +126,172 @@
|
|
126
126
|
|
127
127
|
```
|
128
128
|
|
129
|
+
以下、テキストを投稿機能を作るときに作ったcontroller
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
class PostsController < ApplicationController
|
134
|
+
|
135
|
+
def index
|
136
|
+
|
137
|
+
@posts = Post.all
|
138
|
+
|
139
|
+
@comment = Comment.all
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
def new
|
148
|
+
|
149
|
+
@post = Post.new
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def create
|
156
|
+
|
157
|
+
@post = Post.new(post_params)
|
158
|
+
|
159
|
+
@post.save
|
160
|
+
|
161
|
+
redirect_to action: 'index'
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def post_params
|
170
|
+
|
171
|
+
params.require(:post).permit(:title, :body)
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
いか、画像投稿機能を作るときに作ったcontroller
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
class CommentsController < ApplicationController
|
186
|
+
|
187
|
+
def idndex
|
188
|
+
|
189
|
+
@comment = Comment.all
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
def new
|
196
|
+
|
197
|
+
@comment = Comment.new
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def create
|
204
|
+
|
205
|
+
@comment = Comment.create params.require(:comment).permit(:content, :image) # POINT
|
206
|
+
|
207
|
+
@comment.save
|
208
|
+
|
209
|
+
redirect_to @comment
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def show
|
216
|
+
|
217
|
+
@comment = Comment.find(params[:id])
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
def edit
|
224
|
+
|
225
|
+
@comment = Comment.find(params[:id])
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def update
|
232
|
+
|
233
|
+
@comment = Comment.find(params[:id])
|
234
|
+
|
235
|
+
@comment.update params.require(:comment).permit(:content, :image) # POINT
|
236
|
+
|
237
|
+
redirect_to @comment
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
```
|
244
|
+
|
245
|
+
ログ
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
Processing by PostsController#create as HTML
|
250
|
+
|
251
|
+
web_1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"r53Nupv5tPGPeSQOiMq/tktaETd2Vf0sL712vnlqkoc5gHDvdpiAemcaPPJL4OY55Z/YmHw85xJm6kAP4avMKw==", "post"=>{"title"=>"ddddddd", "body"=>"xxxxxxxxxxxxxx"}, "images"=>["IMG_20201122_180927.jpg"]}
|
252
|
+
|
253
|
+
web_1 | (0.4ms) BEGIN
|
254
|
+
|
255
|
+
web_1 | ↳ app/controllers/posts_controller.rb:14
|
256
|
+
|
257
|
+
web_1 | Post Create (0.6ms) INSERT INTO `posts` (`title`, `body`, `created_at`, `updated_at`) VALUES ('ddddddd', 'xxxxxxxxxxxxxx', '2021-01-28 08:31:27', '2021-01-28 08:31:27')
|
258
|
+
|
259
|
+
web_1 | ↳ app/controllers/posts_controller.rb:14
|
260
|
+
|
261
|
+
web_1 | (2.1ms) COMMIT
|
262
|
+
|
263
|
+
web_1 | ↳ app/controllers/posts_controller.rb:14
|
264
|
+
|
265
|
+
web_1 | Redirected to http://localhost:3000/posts
|
266
|
+
|
267
|
+
web_1 | Completed 302 Found in 15ms (ActiveRecord: 3.1ms)
|
268
|
+
|
269
|
+
web_1 |
|
270
|
+
|
271
|
+
web_1 |
|
272
|
+
|
273
|
+
web_1 | Started GET "/posts" for 172.18.0.1 at 2021-01-28 08:31:27 +0000
|
274
|
+
|
275
|
+
web_1 | Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
|
276
|
+
|
277
|
+
web_1 | Processing by PostsController#index as HTML
|
278
|
+
|
279
|
+
web_1 | Rendering posts/index.html.erb within layouts/application
|
280
|
+
|
281
|
+
web_1 | Comment Load (0.5ms) SELECT `comments`.* FROM `comments`
|
282
|
+
|
283
|
+
web_1 | ↳ app/views/posts/index.html.erb:10
|
284
|
+
|
285
|
+
web_1 | Rendered posts/index.html.erb within layouts/application (3.1ms)
|
286
|
+
|
287
|
+
web_1 | Completed 200 OK in 190ms (Views: 161.2ms | ActiveRecord: 0.5ms)
|
288
|
+
|
289
|
+
web_1 |
|
290
|
+
|
291
|
+
web_1
|
292
|
+
|
293
|
+
```
|
294
|
+
|
129
295
|
#試したこと
|
130
296
|
|
131
297
|
<%= yield %>を投稿詳細ページの方にも追加しました
|