質問編集履歴
1
ソースコード欄にコントローラーとテンプレート(application.html.erb)を書き加えました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rails6
|
1
|
+
rails6で、ログアウトボタンを押すと、routing errorになってしまう
|
test
CHANGED
@@ -38,6 +38,100 @@
|
|
38
38
|
|
39
39
|
ソースコード
|
40
40
|
|
41
|
+
application.html.erb
|
42
|
+
|
43
|
+
<!DOCTYPE html>
|
44
|
+
|
45
|
+
<html>
|
46
|
+
|
47
|
+
<head>
|
48
|
+
|
49
|
+
<title>App1</title>
|
50
|
+
|
51
|
+
<%= csrf_meta_tags %>
|
52
|
+
|
53
|
+
<%= csp_meta_tag %>
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
|
62
|
+
|
63
|
+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
|
64
|
+
|
65
|
+
</head>
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
<body>
|
70
|
+
|
71
|
+
<header>
|
72
|
+
|
73
|
+
<div class="header-logo">
|
74
|
+
|
75
|
+
<%= link_to("メモアプリ(仮)", "/") %>
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<% if @current_user %>
|
80
|
+
|
81
|
+
<ul class="menu">
|
82
|
+
|
83
|
+
<li><a href="#">Menu</a>
|
84
|
+
|
85
|
+
<ul class="sub">
|
86
|
+
|
87
|
+
<li><%= link_to("メモ", "/posts/form") %></li>
|
88
|
+
|
89
|
+
<li><%= link_to("ストップウォッチ", "/") %></li>
|
90
|
+
|
91
|
+
<li><%= link_to("現在日時", "/") %></li>
|
92
|
+
|
93
|
+
<li><%= link_to("ログアウト", "/logout", {method: "post"}) %></li>
|
94
|
+
|
95
|
+
</ul>
|
96
|
+
|
97
|
+
</li>
|
98
|
+
|
99
|
+
<li><%= link_to(@current_user.name, "/users/#{@current_user.id}") %></li>
|
100
|
+
|
101
|
+
</ul>
|
102
|
+
|
103
|
+
<% else %>
|
104
|
+
|
105
|
+
<ul class="menu">
|
106
|
+
|
107
|
+
<li><%= link_to("新規登録", "/signup") %></li>
|
108
|
+
|
109
|
+
<li><%= link_to("ログイン", "/login") %></li>
|
110
|
+
|
111
|
+
</ul>
|
112
|
+
|
113
|
+
<% end %>
|
114
|
+
|
115
|
+
</header>
|
116
|
+
|
117
|
+
<% if flash[:notice] %>
|
118
|
+
|
119
|
+
<div class="flash">
|
120
|
+
|
121
|
+
<%= flash[:notice] %>
|
122
|
+
|
123
|
+
</div>
|
124
|
+
|
125
|
+
<% end %>
|
126
|
+
|
127
|
+
<%= yield %>
|
128
|
+
|
129
|
+
</body>
|
130
|
+
|
131
|
+
</html>
|
132
|
+
|
133
|
+
|
134
|
+
|
41
135
|
routes.rb
|
42
136
|
|
43
137
|
Rails.application.routes.draw do
|
@@ -94,6 +188,200 @@
|
|
94
188
|
|
95
189
|
|
96
190
|
|
191
|
+
users_controller.rb
|
192
|
+
|
193
|
+
class UsersController < ApplicationController
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
before_action :authenticate_user, {only: [:index, :show, :edit, :update]}
|
198
|
+
|
199
|
+
before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
|
200
|
+
|
201
|
+
before_action :ensure_correct_user, {only: [:edit, :update]}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def index
|
206
|
+
|
207
|
+
@users = User.all
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
def show
|
214
|
+
|
215
|
+
@user = User.find_by(id: params[:id])
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
def new
|
222
|
+
|
223
|
+
@user = User.new
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
def create
|
230
|
+
|
231
|
+
@user = User.new(
|
232
|
+
|
233
|
+
name: params[:name],
|
234
|
+
|
235
|
+
email: params[:email],
|
236
|
+
|
237
|
+
image_name: "default.jpg",
|
238
|
+
|
239
|
+
password: params[:password]
|
240
|
+
|
241
|
+
)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if @user.save
|
246
|
+
|
247
|
+
session[:user_id] = @user.id
|
248
|
+
|
249
|
+
flash[:notice] = "ユーザー登録完了!"
|
250
|
+
|
251
|
+
redirect_to("/users/#{@user.id}")
|
252
|
+
|
253
|
+
else
|
254
|
+
|
255
|
+
render("users/new")
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
def edit
|
264
|
+
|
265
|
+
@user = User.find_by(id: params[:id])
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def update
|
272
|
+
|
273
|
+
@user = User.find_by(id: params[:id])
|
274
|
+
|
275
|
+
@user.name = params[:name]
|
276
|
+
|
277
|
+
@user.email = params[:email]
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
if params[:image]
|
282
|
+
|
283
|
+
@user.image_name = "#{@user.id}.jpg"
|
284
|
+
|
285
|
+
image = params[:image]
|
286
|
+
|
287
|
+
File.binwrite("public/user_images/#{@user.image_name}", image.read)
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
if @user.save
|
294
|
+
|
295
|
+
flash[:notice] = "ユーザー情報を編集しました"
|
296
|
+
|
297
|
+
redirect_to("/users/#{@user.id}")
|
298
|
+
|
299
|
+
else
|
300
|
+
|
301
|
+
render("users/edit")
|
302
|
+
|
303
|
+
end
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
def login_form
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def login
|
316
|
+
|
317
|
+
@user = User.find_by(email: params[:email])
|
318
|
+
|
319
|
+
if @user && @user.authenticate(params[:password])
|
320
|
+
|
321
|
+
session[:user_id] = @user.id
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
flash[:notice] = "ログイン成功!"
|
326
|
+
|
327
|
+
redirect_to("/posts/index")
|
328
|
+
|
329
|
+
else
|
330
|
+
|
331
|
+
@error_message = "メールアドレスまたはパスワードが間違っています"
|
332
|
+
|
333
|
+
@email = params[:email]
|
334
|
+
|
335
|
+
@password = params[:password]
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
render("users/login_form")
|
340
|
+
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
def logout
|
348
|
+
|
349
|
+
session[:user_id] = nil
|
350
|
+
|
351
|
+
flash[:notice] = "ログアウトしました"
|
352
|
+
|
353
|
+
redirect_to("/login")
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
def likes
|
360
|
+
|
361
|
+
@user = User.find_by(id: params[:id])
|
362
|
+
|
363
|
+
@likes = Like.where(user_id: @user.id)
|
364
|
+
|
365
|
+
end
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
def ensure_correct_user
|
370
|
+
|
371
|
+
if @current_user.id != params[:id].to_i
|
372
|
+
|
373
|
+
flash[:notice] = "権限がありません"
|
374
|
+
|
375
|
+
redirect_to("/posts/index")
|
376
|
+
|
377
|
+
end
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
|
97
385
|
```
|
98
386
|
|
99
387
|
|