teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記内のの「@post.save」を付け足し。

2020/10/25 05:13

投稿

punchan36
punchan36

スコア105

title CHANGED
File without changes
body CHANGED
@@ -117,6 +117,7 @@
117
117
  post_image = params[:post_image]
118
118
  File.binwrite("public/post_images/#{@post.post_image_name}", post_image.read)
119
119
  end
120
+ @post.save
120
121
  flash[:notice] = "投稿を作成しました"
121
122
  redirect_to("/posts/index")
122
123
  else

1

posts/controller.rbを追記しました。

2020/10/25 05:13

投稿

punchan36
punchan36

スコア105

title CHANGED
File without changes
body CHANGED
@@ -100,4 +100,27 @@
100
100
 
101
101
  ruby 2.6.4p104
102
102
  RubyGems 3.0.3
103
- Rails 5.2.3
103
+ Rails 5.2.3
104
+
105
+ ###【追記】posts/controller.rb
106
+
107
+ ```ruby
108
+ def create
109
+ @post = Post.new(
110
+ title: params[:title],
111
+ post_image_name: "default_post_image.jpg",
112
+ user_id: @current_user.id
113
+ )
114
+ if @post.save
115
+ if params[:post_image]
116
+ @post.post_image_name = "#{@post.id}.jpg"
117
+ post_image = params[:post_image]
118
+ File.binwrite("public/post_images/#{@post.post_image_name}", post_image.read)
119
+ end
120
+ flash[:notice] = "投稿を作成しました"
121
+ redirect_to("/posts/index")
122
+ else
123
+ render("posts/new")
124
+ end
125
+ end
126
+ ```