質問編集履歴
1
ログイン関連のソースコード(users_login_test.rb,sessions_controller_test.rb,sessions_controller.rb)を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -109,6 +109,108 @@
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
```
|
112
|
+
```ruby
|
113
|
+
--sessions_controller.rb--
|
114
|
+
class SessionsController < ApplicationController
|
115
|
+
|
116
|
+
def new
|
117
|
+
end
|
118
|
+
|
119
|
+
def create
|
120
|
+
user = User.find_by(email: params[:session][:email].downcase)
|
121
|
+
if user && user.authenticate(params[:session][:password])
|
122
|
+
if user.activated?
|
123
|
+
log_in user
|
124
|
+
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
|
125
|
+
redirect_back_or user
|
126
|
+
else
|
127
|
+
message = "Account not activated. "
|
128
|
+
message += "Check your email for the activation link."
|
129
|
+
flash[:warning] = message
|
130
|
+
redirect_to root_url
|
131
|
+
end
|
132
|
+
else
|
133
|
+
flash.now[:danger] = 'Invalid email/password combination'
|
134
|
+
render 'new'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def destroy
|
139
|
+
log_out if logged_in?
|
140
|
+
redirect_to root_url
|
141
|
+
end
|
142
|
+
end
|
143
|
+
```
|
144
|
+
```ruby
|
145
|
+
--sessions_controller_test.rb--
|
146
|
+
require 'test_helper'
|
147
|
+
|
148
|
+
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
149
|
+
test "should get new" do
|
150
|
+
get login_path
|
151
|
+
assert_response :success
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
```
|
156
|
+
```ruby
|
157
|
+
--users_login_test.rb--
|
158
|
+
require 'test_helper'
|
159
|
+
|
160
|
+
class UsersLoginTest < ActionDispatch::IntegrationTest
|
161
|
+
def setup
|
162
|
+
@user = users(:michael)
|
163
|
+
end
|
164
|
+
|
165
|
+
test "login with invalid information" do
|
166
|
+
get login_path
|
167
|
+
assert_template 'sessions/new'
|
168
|
+
post login_path, params: { session: { email: "", password: "" } }
|
169
|
+
assert_template 'sessions/new'
|
170
|
+
assert_not flash.empty?
|
171
|
+
get root_path
|
172
|
+
assert flash.empty?
|
173
|
+
end
|
174
|
+
|
175
|
+
test "login with valid information followed by logout" do
|
176
|
+
get login_path
|
177
|
+
post login_path, params: { session: { email: @user.email,
|
178
|
+
password: 'password' } }
|
179
|
+
assert is_logged_in?
|
180
|
+
assert_redirected_to @user
|
181
|
+
follow_redirect!
|
182
|
+
assert_template 'users/show'
|
183
|
+
assert_select "a[href=?]", login_path, count: 0
|
184
|
+
assert_select "a[href=?]", logout_path
|
185
|
+
assert_select "a[href=?]", user_path(@user)
|
186
|
+
|
187
|
+
delete logout_path
|
188
|
+
assert_not is_logged_in?
|
189
|
+
assert_redirected_to root_url
|
190
|
+
# 2番目のウィンドウでログアウトをクリックするユーザーをシミュレートする
|
191
|
+
delete logout_path
|
192
|
+
follow_redirect!
|
193
|
+
assert_select "a[href=?]", login_path
|
194
|
+
assert_select "a[href=?]", logout_path, count: 0
|
195
|
+
assert_select "a[href=?]", user_path(@user), count: 0
|
196
|
+
end
|
197
|
+
|
198
|
+
test "login with remembering" do
|
199
|
+
log_in_as(@user, remember_me: '1')
|
200
|
+
assert_not_empty cookies['remember_token']
|
201
|
+
end
|
202
|
+
|
203
|
+
test "login without remembering" do
|
204
|
+
# クッキーを保存してログイン
|
205
|
+
log_in_as(@user, remember_me: '1')
|
206
|
+
delete logout_path
|
207
|
+
# クッキーを削除してログイン
|
208
|
+
log_in_as(@user, remember_me: '0')
|
209
|
+
assert_empty cookies['remember_token']
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
```
|
112
214
|
[試したこと]
|
113
215
|
・ネットで調べて、undefined method `id'or nil:NilClassエラーはユーザー登録がされていないため表示されているというQ&Aを見たので、ユーザーのsignupを行なったがトラブルシュートできず。
|
114
216
|
・エラーが出ていると思われるソースコードのサンプルコードと照合。
|