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

質問編集履歴

1

ソースコード欄にコントローラーとテンプレート(application.html.erb)を書き加えました。

2020/09/24 08:16

投稿

wk2010plo
wk2010plo

スコア0

title CHANGED
@@ -1,1 +1,1 @@
1
- rails6routing errorについ
1
+ rails6で、ログアウトボタンを押すと、routing errorになっしまう
body CHANGED
@@ -18,6 +18,53 @@
18
18
 
19
19
  ```ここに言語名を入力
20
20
  ソースコード
21
+ application.html.erb
22
+ <!DOCTYPE html>
23
+ <html>
24
+ <head>
25
+ <title>App1</title>
26
+ <%= csrf_meta_tags %>
27
+ <%= csp_meta_tag %>
28
+
29
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
30
+
31
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
32
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
33
+ </head>
34
+
35
+ <body>
36
+ <header>
37
+ <div class="header-logo">
38
+ <%= link_to("メモアプリ(仮)", "/") %>
39
+ </div>
40
+ <% if @current_user %>
41
+ <ul class="menu">
42
+ <li><a href="#">Menu</a>
43
+ <ul class="sub">
44
+ <li><%= link_to("メモ", "/posts/form") %></li>
45
+ <li><%= link_to("ストップウォッチ", "/") %></li>
46
+ <li><%= link_to("現在日時", "/") %></li>
47
+ <li><%= link_to("ログアウト", "/logout", {method: "post"}) %></li>
48
+ </ul>
49
+ </li>
50
+ <li><%= link_to(@current_user.name, "/users/#{@current_user.id}") %></li>
51
+ </ul>
52
+ <% else %>
53
+ <ul class="menu">
54
+ <li><%= link_to("新規登録", "/signup") %></li>
55
+ <li><%= link_to("ログイン", "/login") %></li>
56
+ </ul>
57
+ <% end %>
58
+ </header>
59
+ <% if flash[:notice] %>
60
+ <div class="flash">
61
+ <%= flash[:notice] %>
62
+ </div>
63
+ <% end %>
64
+ <%= yield %>
65
+ </body>
66
+ </html>
67
+
21
68
  routes.rb
22
69
  Rails.application.routes.draw do
23
70
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
@@ -46,6 +93,103 @@
46
93
  get "/" => "home#top"
47
94
  end
48
95
 
96
+ users_controller.rb
97
+ class UsersController < ApplicationController
98
+
99
+ before_action :authenticate_user, {only: [:index, :show, :edit, :update]}
100
+ before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
101
+ before_action :ensure_correct_user, {only: [:edit, :update]}
102
+
103
+ def index
104
+ @users = User.all
105
+ end
106
+
107
+ def show
108
+ @user = User.find_by(id: params[:id])
109
+ end
110
+
111
+ def new
112
+ @user = User.new
113
+ end
114
+
115
+ def create
116
+ @user = User.new(
117
+ name: params[:name],
118
+ email: params[:email],
119
+ image_name: "default.jpg",
120
+ password: params[:password]
121
+ )
122
+
123
+ if @user.save
124
+ session[:user_id] = @user.id
125
+ flash[:notice] = "ユーザー登録完了!"
126
+ redirect_to("/users/#{@user.id}")
127
+ else
128
+ render("users/new")
129
+ end
130
+ end
131
+
132
+ def edit
133
+ @user = User.find_by(id: params[:id])
134
+ end
135
+
136
+ def update
137
+ @user = User.find_by(id: params[:id])
138
+ @user.name = params[:name]
139
+ @user.email = params[:email]
140
+
141
+ if params[:image]
142
+ @user.image_name = "#{@user.id}.jpg"
143
+ image = params[:image]
144
+ File.binwrite("public/user_images/#{@user.image_name}", image.read)
145
+ end
146
+
147
+ if @user.save
148
+ flash[:notice] = "ユーザー情報を編集しました"
149
+ redirect_to("/users/#{@user.id}")
150
+ else
151
+ render("users/edit")
152
+ end
153
+ end
154
+
155
+ def login_form
156
+ end
157
+
158
+ def login
159
+ @user = User.find_by(email: params[:email])
160
+ if @user && @user.authenticate(params[:password])
161
+ session[:user_id] = @user.id
162
+
163
+ flash[:notice] = "ログイン成功!"
164
+ redirect_to("/posts/index")
165
+ else
166
+ @error_message = "メールアドレスまたはパスワードが間違っています"
167
+ @email = params[:email]
168
+ @password = params[:password]
169
+
170
+ render("users/login_form")
171
+ end
172
+ end
173
+
174
+ def logout
175
+ session[:user_id] = nil
176
+ flash[:notice] = "ログアウトしました"
177
+ redirect_to("/login")
178
+ end
179
+
180
+ def likes
181
+ @user = User.find_by(id: params[:id])
182
+ @likes = Like.where(user_id: @user.id)
183
+ end
184
+
185
+ def ensure_correct_user
186
+ if @current_user.id != params[:id].to_i
187
+ flash[:notice] = "権限がありません"
188
+ redirect_to("/posts/index")
189
+ end
190
+ end
191
+ end
192
+
49
193
  ```
50
194
 
51
195
  ### 試したこと