質問編集履歴

1

コントローラーのコードとエラー画面を追記しました。

2018/08/19 12:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -165,3 +165,97 @@
165
165
  </div>
166
166
 
167
167
  ```
168
+
169
+
170
+
171
+ infos_controller.rb
172
+
173
+ ```
174
+
175
+ class InfosController < ApplicationController
176
+
177
+
178
+
179
+ def new
180
+
181
+ @info = Info.new
182
+
183
+ end
184
+
185
+
186
+
187
+ def create
188
+
189
+ @info = Info.new(info_params)
190
+
191
+ if @info.save
192
+
193
+ flash[:success] = '更新しました。'
194
+
195
+ redirect_to edit_info_url
196
+
197
+ else
198
+
199
+ flash.now[:danger] = '更新できませんでした。'
200
+
201
+ render :edit
202
+
203
+ end
204
+
205
+ current_user.info = @info
206
+
207
+ end
208
+
209
+
210
+
211
+ def edit
212
+
213
+ @info = current_user.info
214
+
215
+ end
216
+
217
+
218
+
219
+ def update
220
+
221
+ @info = Info.find_by(user_id: params[:id])
222
+
223
+ if @info.update(info_params)
224
+
225
+ flash[:success] = '更新しました。'
226
+
227
+ redirect_to edit_info_url
228
+
229
+ else
230
+
231
+ flash.now[:danger] = '更新できませんでした。'
232
+
233
+ render :edit
234
+
235
+ end
236
+
237
+ current_user.info = @info
238
+
239
+ end
240
+
241
+
242
+
243
+
244
+
245
+ private
246
+
247
+
248
+
249
+ def info_params
250
+
251
+ params.require(:info).permit(:name, :collage, :department, :sex, :location, :avatar)
252
+
253
+ end
254
+
255
+ end
256
+
257
+ ```
258
+
259
+
260
+
261
+ ![イメージ説明](82384930d748c5a620e16dd0aa1a5d5d.png)