質問編集履歴
1
必要情報を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -198,6 +198,258 @@
|
|
198
198
|
|
199
199
|
```
|
200
200
|
|
201
|
+
ルーティング
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
Rails.application.routes.draw do
|
206
|
+
|
207
|
+
root to: 'top#index'
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
get 'login', to: 'sessions#new'
|
212
|
+
|
213
|
+
post 'login', to: 'sessions#create'
|
214
|
+
|
215
|
+
delete 'logout', to: 'sessions#destroy'
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
get 'signup', to: 'users#new'
|
220
|
+
|
221
|
+
resources :users, only: [:show,:new,:create,:edit,:update,:destroy]
|
222
|
+
|
223
|
+
get 'users/mypersonality/:id', to: 'users#personal'
|
224
|
+
|
225
|
+
get 'users/mypersonality/:id/edit', to:'users#personal_edit'
|
226
|
+
|
227
|
+
patch 'users/mypersonality/:id', to:'users#personal_update'
|
228
|
+
|
229
|
+
put 'users/mypersonality/:id', to: 'users#personal_update'
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
コントローラー
|
238
|
+
|
239
|
+
```
|
240
|
+
|
241
|
+
class UsersController < ApplicationController
|
242
|
+
|
243
|
+
before_action :require_user_logged_in, only: [:index, :show,:edit,:update,:destroy]
|
244
|
+
|
245
|
+
before_action :forbid_login_user, only: [:index,:new,:create]
|
246
|
+
|
247
|
+
before_action :ensure_correct_user, only: [:show,:edit,:update,:personal,:personal_edit]
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def index
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
def show
|
258
|
+
|
259
|
+
@user = User.find(params[:id])
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def new
|
266
|
+
|
267
|
+
@user = User.new
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
def create
|
274
|
+
|
275
|
+
@user = User.new(user_params)
|
276
|
+
|
277
|
+
if @user.save
|
278
|
+
|
279
|
+
flash[:success] = 'ユーザーを登録しました!ログインしましょう'
|
280
|
+
|
281
|
+
redirect_to @user
|
282
|
+
|
283
|
+
else
|
284
|
+
|
285
|
+
flash.now[:danger] = 'ユーザの登録に失敗しました。'
|
286
|
+
|
287
|
+
render :new
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
def edit
|
296
|
+
|
297
|
+
@user = User.find(session[:user_id])
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
def update
|
304
|
+
|
305
|
+
@user = User.find(session[:user_id])
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
if @user.update(user_edit_params)
|
310
|
+
|
311
|
+
flash[:success] = "変更が適用されました!"
|
312
|
+
|
313
|
+
redirect_to @user
|
314
|
+
|
315
|
+
else
|
316
|
+
|
317
|
+
flash.now[:danger] = "変更が適用されませんでした"
|
318
|
+
|
319
|
+
render :edit
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
def destroy
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
def personal
|
334
|
+
|
335
|
+
@user = User.find(session[:user_id])
|
336
|
+
|
337
|
+
end
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
def personal_edit
|
342
|
+
|
343
|
+
@user = User.find(session[:user_id])
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
private
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
def user_params
|
358
|
+
|
359
|
+
params.require(:user).permit(:name, :email, :password, :password_confirmation)
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
def user_edit_params
|
366
|
+
|
367
|
+
params.require(:user).permit(:fullname,:email,:birth,:gender,:postal,:prefecture,:address,:phone,:avator,:income,:bestIncome,:experience,:jobStatus,:totalService,:finalEducation,:schoolName,:department,:period,:hope,:manegiment)
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
def ensure_correct_user
|
374
|
+
|
375
|
+
if current_user.id != params[:id].to_i
|
376
|
+
|
377
|
+
flash[:danger] = "権限がありません"
|
378
|
+
|
379
|
+
redirect_to current_user
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
end
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
end
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
遷移後にエラーとなる新規登録画面
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
<div class="text-center mt-5 mb-5">
|
396
|
+
|
397
|
+
<h3>新規登録</h1>
|
398
|
+
|
399
|
+
</div>
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
<div class="row pb-5">
|
404
|
+
|
405
|
+
<div class="col-md-6 col-md-offset-3 mx-auto mb-5">
|
406
|
+
|
407
|
+
<%= form_for(@user) do |f| %>
|
408
|
+
|
409
|
+
<%= render 'layouts/error_messages', model: f.object %>
|
410
|
+
|
411
|
+
<div class="form-group mb-4">
|
412
|
+
|
413
|
+
<%= f.label :name, 'ユーザーネーム'%>
|
414
|
+
|
415
|
+
<%= f.text_field :name, class: 'form-control' %>
|
416
|
+
|
417
|
+
</div>
|
418
|
+
|
419
|
+
<div class="form-group mb-4">
|
420
|
+
|
421
|
+
<%= f.label :email, 'E-mail'%>
|
422
|
+
|
423
|
+
<%= f.email_field :email, class: 'form-control' %>
|
424
|
+
|
425
|
+
</div>
|
426
|
+
|
427
|
+
<div class="form-group mb-4">
|
428
|
+
|
429
|
+
<%= f.label :password, 'Password'%>
|
430
|
+
|
431
|
+
<%= f.password_field :password, class: 'form-control' %>
|
432
|
+
|
433
|
+
</div>
|
434
|
+
|
435
|
+
<div class="form-group mb-4">
|
436
|
+
|
437
|
+
<%= f.label :password_confirmation, 'Confirm'%>
|
438
|
+
|
439
|
+
<%= f.password_field :password_confirmation, class: 'form-control' %>
|
440
|
+
|
441
|
+
</div>
|
442
|
+
|
443
|
+
<%= f.submit '登録', class: 'btn btn-primary btn-block mt-5' %>
|
444
|
+
|
445
|
+
<% end %>
|
446
|
+
|
447
|
+
</div>
|
448
|
+
|
449
|
+
</div>
|
450
|
+
|
451
|
+
```
|
452
|
+
|
201
453
|
|
202
454
|
|
203
455
|
他に必要な情報があれば追加します。
|