質問編集履歴

4

補足の付け足し

2020/01/27 06:10

投稿

yume0409
yume0409

スコア45

test CHANGED
File without changes
test CHANGED
@@ -191,3 +191,67 @@
191
191
  newは何も定義していないです。
192
192
 
193
193
  よろしくお願いします。
194
+
195
+
196
+
197
+ #補足2
198
+
199
+ 遅くなり、すみません。
200
+
201
+ 回答ありがとうございます!
202
+
203
+ ```
204
+
205
+ def new
206
+
207
+ @tweet = Tweet.new
208
+
209
+ end
210
+
211
+ ```
212
+
213
+ こう付け足してみましたが、先ほどと違うエラーが出てしまいました。
214
+
215
+ エラーはrender 'new'に赤マークが出ていました。
216
+
217
+ ```
218
+
219
+ AbstractController::DoubleRenderError in TweetsController#create
220
+
221
+ Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
222
+
223
+ ```
224
+
225
+ 調べると
226
+
227
+ ```
228
+
229
+ def create
230
+
231
+ @tweet = Tweet.create(text: tweet_params[:text], user_id: current_user.id)
232
+
233
+ redirect_to tweets_path and return
234
+
235
+ if @tweet.save
236
+
237
+ redirect_to :root
238
+
239
+ else
240
+
241
+ render 'new'
242
+
243
+ end
244
+
245
+ end
246
+
247
+ ```
248
+
249
+ redirect_to tweets_pathの後ろにand returnを付け足すとエラーは消えました。
250
+
251
+
252
+
253
+ 今は バリデーションの設定はしてありますので、エラーメッセージは出ないだけでツイート送信したらツイート画面に遷移されますが、ツイートは送信されません。
254
+
255
+ エラーメッセージが出てエラーだったらそのページから動かない設定をしたいです。。。
256
+
257
+ よろしくお願いします。

3

誤字があったから

2020/01/27 06:10

投稿

yume0409
yume0409

スコア45

test CHANGED
File without changes
test CHANGED
@@ -160,15 +160,15 @@
160
160
 
161
161
  redirect_to tweets_path
162
162
 
163
- # if @tweet.save
163
+ if @tweet.save
164
164
 
165
- # redirect_to :root
165
+ redirect_to :root
166
166
 
167
- # else
167
+ else
168
168
 
169
- # render 'new'
169
+ render 'new'
170
170
 
171
- # end
171
+ end
172
172
 
173
173
  end
174
174
 

2

補足を追加したかったから

2020/01/25 06:38

投稿

yume0409
yume0409

スコア45

test CHANGED
File without changes
test CHANGED
@@ -117,3 +117,77 @@
117
117
  バリデーションに関することを調べましたが、どの記事でも同じエラーが出ていて困っています。。。
118
118
 
119
119
  よろしくお願いします。
120
+
121
+
122
+
123
+ #補足
124
+
125
+ tweets_controller.rb
126
+
127
+ ```
128
+
129
+ class TweetsController < ApplicationController
130
+
131
+
132
+
133
+ def index
134
+
135
+ # @tweet = Tweet.find(params[:id])
136
+
137
+ @tweets = Tweet.includes(:user).order("created_at DESC")
138
+
139
+ end
140
+
141
+
142
+
143
+ def show
144
+
145
+ # @tweet = Tweet.find(params[:id])
146
+
147
+ end
148
+
149
+
150
+
151
+ def new
152
+
153
+ end
154
+
155
+
156
+
157
+ def create
158
+
159
+ @tweet = Tweet.create(text: tweet_params[:text], user_id: current_user.id)
160
+
161
+ redirect_to tweets_path
162
+
163
+ # if @tweet.save
164
+
165
+ # redirect_to :root
166
+
167
+ # else
168
+
169
+ # render 'new'
170
+
171
+ # end
172
+
173
+ end
174
+
175
+
176
+
177
+ private
178
+
179
+ def tweet_params
180
+
181
+ params.permit(:text)
182
+
183
+ end
184
+
185
+
186
+
187
+ end
188
+
189
+ ```
190
+
191
+ newは何も定義していないです。
192
+
193
+ よろしくお願いします。

1

タグのつけ忘れ

2020/01/25 06:37

投稿

yume0409
yume0409

スコア45

test CHANGED
File without changes
test CHANGED
File without changes