質問編集履歴
2
追記内のの「@post.save」を付け足し。
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,6 +236,8 @@
|
|
236
236
|
|
237
237
|
end
|
238
238
|
|
239
|
+
@post.save
|
240
|
+
|
239
241
|
flash[:notice] = "投稿を作成しました"
|
240
242
|
|
241
243
|
redirect_to("/posts/index")
|
1
posts/controller.rbを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -203,3 +203,49 @@
|
|
203
203
|
RubyGems 3.0.3
|
204
204
|
|
205
205
|
Rails 5.2.3
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
###【追記】posts/controller.rb
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
|
215
|
+
def create
|
216
|
+
|
217
|
+
@post = Post.new(
|
218
|
+
|
219
|
+
title: params[:title],
|
220
|
+
|
221
|
+
post_image_name: "default_post_image.jpg",
|
222
|
+
|
223
|
+
user_id: @current_user.id
|
224
|
+
|
225
|
+
)
|
226
|
+
|
227
|
+
if @post.save
|
228
|
+
|
229
|
+
if params[:post_image]
|
230
|
+
|
231
|
+
@post.post_image_name = "#{@post.id}.jpg"
|
232
|
+
|
233
|
+
post_image = params[:post_image]
|
234
|
+
|
235
|
+
File.binwrite("public/post_images/#{@post.post_image_name}", post_image.read)
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
flash[:notice] = "投稿を作成しました"
|
240
|
+
|
241
|
+
redirect_to("/posts/index")
|
242
|
+
|
243
|
+
else
|
244
|
+
|
245
|
+
render("posts/new")
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
```
|