質問編集履歴
6
最新のコードへ変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,65 +54,105 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
-
####
|
58
|
-
|
59
|
-
```r
|
60
|
-
|
61
|
-
<% i
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
57
|
+
#### drinks/index.html.erb
|
58
|
+
|
59
|
+
```erb
|
60
|
+
|
61
|
+
<%= link_to "退会する", user_path(current_user),method: :delete %>
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
current_userは自分で定義したヘルパーメソッドです
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
#### sessions_helper.rb
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
|
75
|
+
# 現在ログインしているユーザーの情報を取得
|
76
|
+
|
77
|
+
def current_user
|
78
|
+
|
79
|
+
# DBの問い合わせの数を可能な限り小さくしたい
|
80
|
+
|
81
|
+
# logged_in?メソッドでも使われてるし、、、
|
82
|
+
|
83
|
+
if user_id = session[:user_id]
|
84
|
+
|
85
|
+
# セッションがある場合
|
86
|
+
|
87
|
+
# すなわちログインしてる時のみ
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
# sessionにアクセスした結果を変数に
|
92
|
+
|
93
|
+
# 入れておいてあとで使いまわした方が
|
94
|
+
|
95
|
+
# 早くなる
|
96
|
+
|
97
|
+
@current_user ||= User.find_by(id: user_id)
|
98
|
+
|
99
|
+
# find_byでデータベースにクエリを投げる
|
100
|
+
|
101
|
+
# ブラウザのセッションにあるuser_idをもとにUser定義
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# find_byの実行結果をインスタンス変数に保存する
|
106
|
+
|
107
|
+
# ことで、1リクエスト内におけるデータベースへの
|
108
|
+
|
109
|
+
# 問い合わせは最初の一回だけになり、
|
110
|
+
|
111
|
+
# 以後の呼び出しではインスタンス変数の結果を
|
112
|
+
|
113
|
+
# 再利用する
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# すでに@current_userが存在する場合って何?
|
118
|
+
|
119
|
+
# 一回current_userを実行したら、
|
120
|
+
|
121
|
+
# @current_userがあるのでそれを使ってね
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# sessionのuser_idがあるということは
|
126
|
+
|
127
|
+
# 既にログインしてるといてDBにユーザーの情報があるはず。
|
128
|
+
|
129
|
+
# だからsessionのuser_idをDBでfind_byかければいい
|
130
|
+
|
131
|
+
elsif (user_id = cookies.signed[:user_id])
|
132
|
+
|
133
|
+
# sessionが張られてなかったらcookiesにあるかも
|
134
|
+
|
135
|
+
user = User.find_by(id: user_id)
|
136
|
+
|
137
|
+
if user && user.authenticated?(cookies[:remember_token])
|
138
|
+
|
139
|
+
# nilガード
|
140
|
+
|
141
|
+
# クッキーのuser_idとremember_tokenが一致してる
|
142
|
+
|
143
|
+
log_in user
|
144
|
+
|
145
|
+
@current_user = user
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
116
156
|
|
117
157
|
#### user.rb
|
118
158
|
|
@@ -130,7 +170,7 @@
|
|
130
170
|
|
131
171
|
# u.remember_token的なことができる
|
132
172
|
|
133
|
-
has_many :drinks, dependent: :de
|
173
|
+
has_many :drinks, dependent: :delete_all
|
134
174
|
|
135
175
|
before_save { self.email = email.downcase }
|
136
176
|
|
5
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -334,6 +334,32 @@
|
|
334
334
|
|
335
335
|
|
336
336
|
|
337
|
+
#### routes.rb
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
```ruby
|
342
|
+
|
343
|
+
Rails.application.routes.draw do
|
344
|
+
|
345
|
+
root to: 'drinks#index'
|
346
|
+
|
347
|
+
get '/login', to: 'sessions#new'
|
348
|
+
|
349
|
+
post '/login', to: 'sessions#create'
|
350
|
+
|
351
|
+
delete '/logout', to: 'sessions#destroy'
|
352
|
+
|
353
|
+
resources :users
|
354
|
+
|
355
|
+
resources :drinks, only: [:index,:new,:show,:create,:destroy]
|
356
|
+
|
357
|
+
end
|
358
|
+
|
359
|
+
```
|
360
|
+
|
361
|
+
|
362
|
+
|
337
363
|
|
338
364
|
|
339
365
|
|
4
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,136 +28,304 @@
|
|
28
28
|
|
29
29
|
include SessionsHelper
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
e
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def destroy
|
36
|
+
|
37
|
+
user = User.find(params[:id])
|
38
|
+
|
39
|
+
#binding.pry
|
40
|
+
|
41
|
+
user.destroy
|
42
|
+
|
43
|
+
redirect_to root_url
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
#### users/new.html.erb
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
|
61
|
+
<% if current_user %>
|
62
|
+
|
63
|
+
<h1>welcome to my app</h1>
|
64
|
+
|
65
|
+
<% else %>
|
66
|
+
|
67
|
+
<%= link_to "ログイン", login_path%>
|
68
|
+
|
69
|
+
<% end %>
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
<h2><%= link_to "ユーザー一覧",users_path %></h2>
|
76
|
+
|
77
|
+
<% unless logged_in? %>
|
78
|
+
|
79
|
+
<div class="col-md-6 col-md-offset-3">
|
80
|
+
|
81
|
+
<%= render 'shared/form' %>
|
82
|
+
|
83
|
+
</div>
|
84
|
+
|
85
|
+
<% end %>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
<% if current_user %>
|
94
|
+
|
95
|
+
<p>ログインしてます</p>
|
96
|
+
|
97
|
+
<%= link_to @current_user.nickname, user_path(@current_user) %>
|
98
|
+
|
99
|
+
<%= link_to "ログアウト",logout_path,method: :delete %>
|
100
|
+
|
101
|
+
<%= link_to "投稿する", new_drink_path %>
|
102
|
+
|
103
|
+
<%= link_to "退会する", user_path(current_user),method: :delete %>
|
104
|
+
|
105
|
+
<% else %>
|
106
|
+
|
107
|
+
<strong>ログインしてません</strong>
|
108
|
+
|
109
|
+
<% end %>
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
#### user.rb
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
|
121
|
+
class User < ApplicationRecord
|
122
|
+
|
123
|
+
attr_accessor :remember_token
|
124
|
+
|
125
|
+
# remember_tokenというメソッドを作成
|
126
|
+
|
127
|
+
# password,password_comfirmationみたいな
|
128
|
+
|
129
|
+
# 変数のように扱える
|
130
|
+
|
131
|
+
# u.remember_token的なことができる
|
132
|
+
|
133
|
+
has_many :drinks, dependent: :destroy
|
134
|
+
|
135
|
+
before_save { self.email = email.downcase }
|
136
|
+
|
137
|
+
has_secure_password
|
138
|
+
|
139
|
+
validates :nickname, presence: true, length: { maximum: 50 }
|
140
|
+
|
141
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
142
|
+
|
143
|
+
validates :email, presence: true, length: { maximum: 255 },
|
144
|
+
|
145
|
+
format: { with: VALID_EMAIL_REGEX },
|
146
|
+
|
147
|
+
uniqueness: { case_sensitive: false }
|
148
|
+
|
149
|
+
validates :password, presence: true, length: { minimum: 6 },allow_nil: true
|
150
|
+
|
151
|
+
# ユーザー更新時に空のパスワードでも大丈夫
|
152
|
+
|
153
|
+
# has_secure_passwordの方でpasswordの存在性を検証するから大丈夫
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# 渡された文字列のハッシュ値を返す
|
162
|
+
|
163
|
+
def User.digest(string)
|
164
|
+
|
165
|
+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
166
|
+
|
167
|
+
BCrypt::Engine.cost
|
168
|
+
|
169
|
+
BCrypt::Password.create(string, cost: cost)
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
# ランダムなトークンを返す
|
176
|
+
|
177
|
+
def User.new_token
|
178
|
+
|
179
|
+
SecureRandom.urlsafe_base64
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
# 永続的にログインするためにトークンをDBに保存
|
186
|
+
|
187
|
+
def remember
|
188
|
+
|
189
|
+
self.remember_token = User.new_token
|
190
|
+
|
191
|
+
# 何のトークンか分かりやすいから
|
192
|
+
|
193
|
+
# remember_tokenって名前を作った
|
194
|
+
|
195
|
+
update_attribute(:remember_digest,User.digest(remember_token))
|
196
|
+
|
197
|
+
# ハッシュ化したトークンをremember_digestに保存
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
# 渡されたトークンがダイジェストと一致したらtrue
|
204
|
+
|
205
|
+
# を返す
|
206
|
+
|
207
|
+
def authenticated?(remember_token)
|
208
|
+
|
209
|
+
return false if remember_digest.nil?
|
210
|
+
|
211
|
+
# 二種類のブラウザを使用してログアウトした場合
|
212
|
+
|
213
|
+
# cookiesのremember_tokenはあるけど、
|
214
|
+
|
215
|
+
# サーバー側でremember_digestをnilにしてるから
|
216
|
+
|
217
|
+
# nilに対して.is_password?とかやるとfor nil classエラーが起きちゃう
|
218
|
+
|
219
|
+
# remember_digestがnilの場合はfalseを返して、処理を止める
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
# 後置if文に当てはまる条件があれば処理を止めて!
|
224
|
+
|
225
|
+
# って場合はreturnとかで明示的に書くとif ~ else ~ end
|
226
|
+
|
227
|
+
# とか書かなくて済む
|
228
|
+
|
229
|
+
BCrypt::Password.new(remember_digest).is_password?(remember_token)
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
# ユーザーのログイン情報を破棄する
|
236
|
+
|
237
|
+
def forget
|
238
|
+
|
239
|
+
update_attribute(:remember_digest,nil)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
#### Drink.rb
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
|
257
|
+
class Drink < ApplicationRecord
|
258
|
+
|
259
|
+
belongs_to :user
|
260
|
+
|
261
|
+
with_options presence: true do
|
262
|
+
|
263
|
+
validates :name
|
264
|
+
|
265
|
+
validates :explain
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
#### :20201116073717_create_users.rb
|
276
|
+
|
277
|
+
```ruby20201116073717_create_users.rb
|
278
|
+
|
279
|
+
class CreateUsers < ActiveRecord::Migration[6.0]
|
280
|
+
|
281
|
+
def change
|
282
|
+
|
283
|
+
create_table :users do |t|
|
284
|
+
|
285
|
+
t.string :nickname
|
286
|
+
|
287
|
+
t.string :email
|
288
|
+
|
289
|
+
t.string :password_digest
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
t.timestamps
|
68
294
|
|
69
295
|
end
|
70
296
|
|
71
297
|
end
|
72
298
|
|
73
|
-
|
74
|
-
|
75
|
-
def show
|
76
|
-
|
77
|
-
@drinks = current_user.drinks
|
78
|
-
|
79
|
-
|
299
|
+
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
300
|
+
|
84
|
-
|
85
|
-
|
301
|
+
```
|
302
|
+
|
303
|
+
|
304
|
+
|
86
|
-
|
305
|
+
#### /20201118162753_create_drinks.rb
|
306
|
+
|
87
|
-
|
307
|
+
```ruby
|
308
|
+
|
88
|
-
|
309
|
+
class CreateDrinks < ActiveRecord::Migration[6.0]
|
310
|
+
|
89
|
-
def
|
311
|
+
def change
|
312
|
+
|
90
|
-
|
313
|
+
create_table :drinks do |t|
|
314
|
+
|
315
|
+
t.string :name
|
316
|
+
|
317
|
+
t.integer :price
|
318
|
+
|
319
|
+
t.text :explain
|
320
|
+
|
91
|
-
|
321
|
+
t.references :user,null: false,foreign_key: true
|
92
|
-
|
322
|
+
|
93
|
-
|
323
|
+
t.timestamps
|
94
|
-
|
95
|
-
else
|
96
|
-
|
97
|
-
render
|
98
324
|
|
99
325
|
end
|
100
326
|
|
101
327
|
end
|
102
328
|
|
103
|
-
|
104
|
-
|
105
|
-
def destroy
|
106
|
-
|
107
|
-
user = User.find(params[:id])
|
108
|
-
|
109
|
-
#binding.pry
|
110
|
-
|
111
|
-
user.destroy
|
112
|
-
|
113
|
-
redirect_to root_url
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def user_params
|
122
|
-
|
123
|
-
params.require(:user).permit(:nickname,:email,:password,:password_confirmation)
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
def set_user
|
130
|
-
|
131
|
-
@user = User.find(params[:id])
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
def logged_in_user
|
138
|
-
|
139
|
-
unless logged_in?
|
140
|
-
|
141
|
-
store_location
|
142
|
-
|
143
|
-
# ユーザーがいきたがってたページを記憶
|
144
|
-
|
145
|
-
flash[:danger] = "Please log in"
|
146
|
-
|
147
|
-
redirect_to login_url
|
148
|
-
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
def correct_user
|
156
|
-
|
157
|
-
redirect_to(root_url) unless current_user?(@user)
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
329
|
end
|
162
330
|
|
163
331
|
|
@@ -166,415 +334,7 @@
|
|
166
334
|
|
167
335
|
|
168
336
|
|
169
|
-
|
170
|
-
|
171
|
-
```ruby
|
172
|
-
|
173
|
-
<% if current_user %>
|
174
|
-
|
175
|
-
<h1>welcome to my app</h1>
|
176
|
-
|
177
|
-
<% else %>
|
178
|
-
|
179
|
-
<%= link_to "ログイン", login_path%>
|
180
|
-
|
181
|
-
<% end %>
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
<h2><%= link_to "ユーザー一覧",users_path %></h2>
|
188
|
-
|
189
|
-
<% unless logged_in? %>
|
190
|
-
|
191
|
-
<div class="col-md-6 col-md-offset-3">
|
192
|
-
|
193
|
-
<%= render 'shared/form' %>
|
194
|
-
|
195
|
-
</div>
|
196
|
-
|
197
|
-
<% end %>
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
<% if current_user %>
|
206
|
-
|
207
|
-
<p>ログインしてます</p>
|
208
|
-
|
209
|
-
<%= link_to @current_user.nickname, user_path(@current_user) %>
|
210
|
-
|
211
|
-
<%= link_to "ログアウト",logout_path,method: :delete %>
|
212
|
-
|
213
|
-
<%= link_to "投稿する", new_drink_path %>
|
214
|
-
|
215
|
-
<%= link_to "退会する", user_path(current_user),method: :delete %>
|
216
|
-
|
217
|
-
<% else %>
|
218
|
-
|
219
|
-
<strong>ログインしてません</strong>
|
220
|
-
|
221
|
-
<% end %>
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
```
|
228
|
-
|
229
|
-
#### user.rb
|
230
|
-
|
231
|
-
```ruby
|
232
|
-
|
233
|
-
class User < ApplicationRecord
|
234
|
-
|
235
|
-
attr_accessor :remember_token
|
236
|
-
|
237
|
-
# remember_tokenというメソッドを作成
|
238
|
-
|
239
|
-
# password,password_comfirmationみたいな
|
240
|
-
|
241
|
-
# 変数のように扱える
|
242
|
-
|
243
|
-
# u.remember_token的なことができる
|
244
|
-
|
245
|
-
has_many :drinks, dependent: :destroy
|
246
|
-
|
247
|
-
before_save { self.email = email.downcase }
|
248
|
-
|
249
|
-
has_secure_password
|
250
|
-
|
251
|
-
validates :nickname, presence: true, length: { maximum: 50 }
|
252
|
-
|
253
|
-
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
254
|
-
|
255
|
-
validates :email, presence: true, length: { maximum: 255 },
|
256
|
-
|
257
|
-
format: { with: VALID_EMAIL_REGEX },
|
258
|
-
|
259
|
-
uniqueness: { case_sensitive: false }
|
260
|
-
|
261
|
-
validates :password, presence: true, length: { minimum: 6 },allow_nil: true
|
262
|
-
|
263
|
-
# ユーザー更新時に空のパスワードでも大丈夫
|
264
|
-
|
265
|
-
# has_secure_passwordの方でpasswordの存在性を検証するから大丈夫
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
# 渡された文字列のハッシュ値を返す
|
274
|
-
|
275
|
-
def User.digest(string)
|
276
|
-
|
277
|
-
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
278
|
-
|
279
|
-
BCrypt::Engine.cost
|
280
|
-
|
281
|
-
BCrypt::Password.create(string, cost: cost)
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
# ランダムなトークンを返す
|
288
|
-
|
289
|
-
def User.new_token
|
290
|
-
|
291
|
-
SecureRandom.urlsafe_base64
|
292
|
-
|
293
|
-
end
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
# 永続的にログインするためにトークンをDBに保存
|
298
|
-
|
299
|
-
def remember
|
300
|
-
|
301
|
-
self.remember_token = User.new_token
|
302
|
-
|
303
|
-
# 何のトークンか分かりやすいから
|
304
|
-
|
305
|
-
# remember_tokenって名前を作った
|
306
|
-
|
307
|
-
update_attribute(:remember_digest,User.digest(remember_token))
|
308
|
-
|
309
|
-
# ハッシュ化したトークンをremember_digestに保存
|
310
|
-
|
311
|
-
end
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
# 渡されたトークンがダイジェストと一致したらtrue
|
316
|
-
|
317
|
-
# を返す
|
318
|
-
|
319
|
-
def authenticated?(remember_token)
|
320
|
-
|
321
|
-
return false if remember_digest.nil?
|
322
|
-
|
323
|
-
# 二種類のブラウザを使用してログアウトした場合
|
324
|
-
|
325
|
-
# cookiesのremember_tokenはあるけど、
|
326
|
-
|
327
|
-
# サーバー側でremember_digestをnilにしてるから
|
328
|
-
|
329
|
-
# nilに対して.is_password?とかやるとfor nil classエラーが起きちゃう
|
330
|
-
|
331
|
-
# remember_digestがnilの場合はfalseを返して、処理を止める
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
# 後置if文に当てはまる条件があれば処理を止めて!
|
336
|
-
|
337
|
-
# って場合はreturnとかで明示的に書くとif ~ else ~ end
|
338
|
-
|
339
|
-
# とか書かなくて済む
|
340
|
-
|
341
|
-
BCrypt::Password.new(remember_digest).is_password?(remember_token)
|
342
|
-
|
343
|
-
end
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
# ユーザーのログイン情報を破棄する
|
348
|
-
|
349
|
-
def forget
|
350
|
-
|
351
|
-
update_attribute(:remember_digest,nil)
|
352
|
-
|
353
|
-
end
|
354
|
-
|
355
|
-
end
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
```
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
#### Drink.rb
|
366
|
-
|
367
|
-
```ruby
|
368
|
-
|
369
|
-
class Drink < ApplicationRecord
|
370
|
-
|
371
|
-
belongs_to :user
|
372
|
-
|
373
|
-
with_options presence: true do
|
374
|
-
|
375
|
-
validates :name
|
376
|
-
|
377
|
-
validates :explain
|
378
|
-
|
379
|
-
end
|
380
|
-
|
381
|
-
end
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
```
|
386
|
-
|
387
|
-
#### :20201116073717_create_users.rb
|
388
|
-
|
389
|
-
```ruby20201116073717_create_users.rb
|
390
|
-
|
391
|
-
class CreateUsers < ActiveRecord::Migration[6.0]
|
392
|
-
|
393
|
-
def change
|
394
|
-
|
395
|
-
create_table :users do |t|
|
396
|
-
|
397
|
-
t.string :nickname
|
398
|
-
|
399
|
-
t.string :email
|
400
|
-
|
401
|
-
t.string :password_digest
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
t.timestamps
|
406
|
-
|
407
|
-
end
|
408
|
-
|
409
|
-
end
|
410
|
-
|
411
|
-
end
|
412
|
-
|
413
|
-
```
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
#### /20201118162753_create_drinks.rb
|
418
|
-
|
419
|
-
```ruby
|
420
|
-
|
421
|
-
class CreateDrinks < ActiveRecord::Migration[6.0]
|
422
|
-
|
423
|
-
def change
|
424
|
-
|
425
|
-
create_table :drinks do |t|
|
426
|
-
|
427
|
-
t.string :name
|
428
|
-
|
429
|
-
t.integer :price
|
430
|
-
|
431
|
-
t.text :explain
|
432
|
-
|
433
|
-
t.references :user,null: false,foreign_key: true
|
434
|
-
|
435
|
-
t.timestamps
|
436
|
-
|
437
|
-
end
|
438
|
-
|
439
|
-
end
|
440
|
-
|
441
|
-
end
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
```
|
446
|
-
|
447
|
-
### 試したこと
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
binding.pryで
|
452
|
-
|
453
|
-
```ruby
|
454
|
-
|
455
|
-
def destroy
|
456
|
-
|
457
|
-
user = User.find(params[:id])
|
458
|
-
|
459
|
-
#binding.pry
|
460
|
-
|
461
|
-
user.destroy
|
462
|
-
|
463
|
-
redirect_to root_url
|
464
|
-
|
465
|
-
end
|
466
|
-
|
467
|
-
```
|
468
|
-
|
469
|
-
で
|
470
|
-
|
471
|
-
```
|
472
|
-
|
473
|
-
user
|
474
|
-
|
475
|
-
```
|
476
|
-
|
477
|
-
を実行したら
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
```
|
482
|
-
|
483
|
-
#<User:0x00007f98acc0f778
|
484
|
-
|
485
|
-
id: 23,
|
486
|
-
|
487
|
-
nickname: "そうちゃん",
|
488
|
-
|
489
|
-
email: "hogehoge@gmail.com",
|
490
|
-
|
491
|
-
created_at: Thu, 19 Nov 2020 08:00:16 UTC +00:00,
|
492
|
-
|
493
|
-
updated_at: Mon, 23 Nov 2020 07:11:57 UTC +00:00,
|
494
|
-
|
495
|
-
password_digest: [FILTERED],
|
496
|
-
|
497
|
-
remember_digest:
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
```
|
502
|
-
|
503
|
-
でちゃんとuserの情報は受け渡されてるように思えるのですが
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
```
|
508
|
-
|
509
|
-
user.destroy
|
510
|
-
|
511
|
-
```
|
512
|
-
|
513
|
-
を実行すると
|
514
|
-
|
515
|
-
```
|
516
|
-
|
517
|
-
(0.3ms) BEGIN
|
518
|
-
|
519
|
-
↳ (pry):9:in `destroy'
|
520
|
-
|
521
|
-
Drink Destroy (0.3ms) DELETE FROM `drinks` WHERE `drinks`.`id` = 1
|
522
|
-
|
523
|
-
↳ (pry):9:in `destroy'
|
524
|
-
|
525
|
-
(0.8ms) ROLLBACK
|
526
|
-
|
527
|
-
↳ (pry):9:in `destroy'
|
528
|
-
|
529
|
-
=> false
|
530
|
-
|
531
|
-
[7] pry(#<UsersController>)> exit
|
532
|
-
|
533
|
-
(0.2ms) BEGIN
|
534
|
-
|
535
|
-
↳ app/controllers/users_controller.rb:43:in `destroy'
|
536
|
-
|
537
|
-
Drink Destroy (0.3ms) DELETE FROM `drinks` WHERE `drinks`.`id` = 1
|
538
|
-
|
539
|
-
↳ app/controllers/users_controller.rb:43:in `destroy'
|
540
|
-
|
541
|
-
(2.5ms) ROLLBACK
|
542
|
-
|
543
|
-
↳ app/controllers/users_controller.rb:43:in `destroy'
|
544
|
-
|
545
|
-
Redirected to http://localhost:3000/
|
546
|
-
|
547
|
-
Completed 302 Found in 487105ms (ActiveRecord: 9.0ms | Allocations: 53697)
|
548
|
-
|
549
|
-
```
|
550
|
-
|
551
|
-
このようなログになりました
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
#### rails sのログ
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
```
|
560
|
-
|
561
|
-
Redirected to http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6/%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F2.jpg%22%3B+filename%2A%3DUTF-8%27%27%25E3%2582%25B3%25E3%2583%25BC%25E3%2583%2592%25E3%2583%25BC%25E3%2583%258F%25E3%2582%259A%25E3%2582%25B9%25E3%2583%259B%25E3%2582%259A%25E3%2583%25BC%25E3%2583%25882.jpg
|
562
|
-
|
563
|
-
Completed 302 Found in 5ms (ActiveRecord: 0.4ms | Allocations: 2111)
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6/%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F2.jpg%22%3B+filename%2A%3DUTF-8%27%27%25E3%2582%25B3%25E3%2583%25BC%25E3%2583%2592%25E3%2583%25BC%25E3%2583%258F%25E3%2582%259A%25E3%2582%25B9%25E3%2583%259B%25E3%2582%259A%25E3%2583%25BC%25E3%2583%25882.jpg" for ::1 at 2020-11-27 09:28:22 +0900
|
570
|
-
|
571
|
-
Processing by ActiveStorage::DiskController#show as JPEG
|
572
|
-
|
573
|
-
Parameters: {"content_type"=>"image/jpeg", "disposition"=>"inline; filename=\"%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F2.jpg\"; filename*=UTF-8''%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6", "filename"=>"コーヒーパスポート2"}
|
574
|
-
|
575
|
-
Completed 200 OK in 1ms (ActiveRecord: 0.0ms | Allocations: 395)
|
576
|
-
|
577
|
-
```
|
337
|
+
|
578
338
|
|
579
339
|
|
580
340
|
|
3
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -384,6 +384,66 @@
|
|
384
384
|
|
385
385
|
```
|
386
386
|
|
387
|
+
#### :20201116073717_create_users.rb
|
388
|
+
|
389
|
+
```ruby20201116073717_create_users.rb
|
390
|
+
|
391
|
+
class CreateUsers < ActiveRecord::Migration[6.0]
|
392
|
+
|
393
|
+
def change
|
394
|
+
|
395
|
+
create_table :users do |t|
|
396
|
+
|
397
|
+
t.string :nickname
|
398
|
+
|
399
|
+
t.string :email
|
400
|
+
|
401
|
+
t.string :password_digest
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
t.timestamps
|
406
|
+
|
407
|
+
end
|
408
|
+
|
409
|
+
end
|
410
|
+
|
411
|
+
end
|
412
|
+
|
413
|
+
```
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
#### /20201118162753_create_drinks.rb
|
418
|
+
|
419
|
+
```ruby
|
420
|
+
|
421
|
+
class CreateDrinks < ActiveRecord::Migration[6.0]
|
422
|
+
|
423
|
+
def change
|
424
|
+
|
425
|
+
create_table :drinks do |t|
|
426
|
+
|
427
|
+
t.string :name
|
428
|
+
|
429
|
+
t.integer :price
|
430
|
+
|
431
|
+
t.text :explain
|
432
|
+
|
433
|
+
t.references :user,null: false,foreign_key: true
|
434
|
+
|
435
|
+
t.timestamps
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
end
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
```
|
446
|
+
|
387
447
|
### 試したこと
|
388
448
|
|
389
449
|
|
@@ -492,7 +552,41 @@
|
|
492
552
|
|
493
553
|
|
494
554
|
|
495
|
-
|
555
|
+
#### rails sのログ
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
```
|
560
|
+
|
561
|
+
Redirected to http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6/%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F2.jpg%22%3B+filename%2A%3DUTF-8%27%27%25E3%2582%25B3%25E3%2583%25BC%25E3%2583%2592%25E3%2583%25BC%25E3%2583%258F%25E3%2582%259A%25E3%2582%25B9%25E3%2583%259B%25E3%2582%259A%25E3%2583%25BC%25E3%2583%25882.jpg
|
562
|
+
|
563
|
+
Completed 302 Found in 5ms (ActiveRecord: 0.4ms | Allocations: 2111)
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6/%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F%253F2.jpg%22%3B+filename%2A%3DUTF-8%27%27%25E3%2582%25B3%25E3%2583%25BC%25E3%2583%2592%25E3%2583%25BC%25E3%2583%258F%25E3%2582%259A%25E3%2582%25B9%25E3%2583%259B%25E3%2582%259A%25E3%2583%25BC%25E3%2583%25882.jpg" for ::1 at 2020-11-27 09:28:22 +0900
|
570
|
+
|
571
|
+
Processing by ActiveStorage::DiskController#show as JPEG
|
572
|
+
|
573
|
+
Parameters: {"content_type"=>"image/jpeg", "disposition"=>"inline; filename=\"%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F2.jpg\"; filename*=UTF-8''%E3%82%B3%E3%83%BC%E3%83%92%E3%83%BC%E3%83%8F%E3%82%9A%E3%82%B9%E3%83%9B%E3%82%9A%E3%83%BC%E3%83%882.jpg", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhjV3hxT1dob2RqbHhiREkzWm5sMWFHVXdjVGxqY1dGcVl6WTFZUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpQWJScGJteHBibVU3SUdacGJHVnVZVzFsUFNJbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFlsTTBZbE0wWWxNMFl5TG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25KVVV6SlRneUpVSXpKVVV6SlRnekpVSkRKVVV6SlRnekpUa3lKVVV6SlRnekpVSkRKVVV6SlRnekpUaEdKVVV6SlRneUpUbEJKVVV6SlRneUpVSTVKVVV6SlRnekpUbENKVVV6SlRneUpUbEJKVVV6SlRnekpVSkRKVVV6SlRnekpUZzRNaTVxY0djR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dWSklnOXBiV0ZuWlM5cWNHVm5CanNHVkE9PSIsImV4cCI6IjIwMjAtMTEtMjdUMDA6MzM6MjEuOTk2WiIsInB1ciI6ImJsb2Jfa2V5In19--ece506b9c100e0b36f643e52e8a71a124990f1f6", "filename"=>"コーヒーパスポート2"}
|
574
|
+
|
575
|
+
Completed 200 OK in 1ms (ActiveRecord: 0.0ms | Allocations: 395)
|
576
|
+
|
577
|
+
```
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
退会リンクをusers/showに移動しました
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
エラーも起きていませんし、@userの定義もしっかりできていて、@user.nameとかやってもしっかり表示できているので、リンクの指定はあってると思います
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
このログを見るとリンクを踏んだ後にrootに戻ってるように見えます
|
496
590
|
|
497
591
|
|
498
592
|
|
2
最新のコードへ変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -368,7 +368,7 @@
|
|
368
368
|
|
369
369
|
class Drink < ApplicationRecord
|
370
370
|
|
371
|
-
belongs_to :user
|
371
|
+
belongs_to :user
|
372
372
|
|
373
373
|
with_options presence: true do
|
374
374
|
|
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -226,6 +226,164 @@
|
|
226
226
|
|
227
227
|
```
|
228
228
|
|
229
|
+
#### user.rb
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
|
233
|
+
class User < ApplicationRecord
|
234
|
+
|
235
|
+
attr_accessor :remember_token
|
236
|
+
|
237
|
+
# remember_tokenというメソッドを作成
|
238
|
+
|
239
|
+
# password,password_comfirmationみたいな
|
240
|
+
|
241
|
+
# 変数のように扱える
|
242
|
+
|
243
|
+
# u.remember_token的なことができる
|
244
|
+
|
245
|
+
has_many :drinks, dependent: :destroy
|
246
|
+
|
247
|
+
before_save { self.email = email.downcase }
|
248
|
+
|
249
|
+
has_secure_password
|
250
|
+
|
251
|
+
validates :nickname, presence: true, length: { maximum: 50 }
|
252
|
+
|
253
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
254
|
+
|
255
|
+
validates :email, presence: true, length: { maximum: 255 },
|
256
|
+
|
257
|
+
format: { with: VALID_EMAIL_REGEX },
|
258
|
+
|
259
|
+
uniqueness: { case_sensitive: false }
|
260
|
+
|
261
|
+
validates :password, presence: true, length: { minimum: 6 },allow_nil: true
|
262
|
+
|
263
|
+
# ユーザー更新時に空のパスワードでも大丈夫
|
264
|
+
|
265
|
+
# has_secure_passwordの方でpasswordの存在性を検証するから大丈夫
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
# 渡された文字列のハッシュ値を返す
|
274
|
+
|
275
|
+
def User.digest(string)
|
276
|
+
|
277
|
+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
278
|
+
|
279
|
+
BCrypt::Engine.cost
|
280
|
+
|
281
|
+
BCrypt::Password.create(string, cost: cost)
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
# ランダムなトークンを返す
|
288
|
+
|
289
|
+
def User.new_token
|
290
|
+
|
291
|
+
SecureRandom.urlsafe_base64
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
# 永続的にログインするためにトークンをDBに保存
|
298
|
+
|
299
|
+
def remember
|
300
|
+
|
301
|
+
self.remember_token = User.new_token
|
302
|
+
|
303
|
+
# 何のトークンか分かりやすいから
|
304
|
+
|
305
|
+
# remember_tokenって名前を作った
|
306
|
+
|
307
|
+
update_attribute(:remember_digest,User.digest(remember_token))
|
308
|
+
|
309
|
+
# ハッシュ化したトークンをremember_digestに保存
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
# 渡されたトークンがダイジェストと一致したらtrue
|
316
|
+
|
317
|
+
# を返す
|
318
|
+
|
319
|
+
def authenticated?(remember_token)
|
320
|
+
|
321
|
+
return false if remember_digest.nil?
|
322
|
+
|
323
|
+
# 二種類のブラウザを使用してログアウトした場合
|
324
|
+
|
325
|
+
# cookiesのremember_tokenはあるけど、
|
326
|
+
|
327
|
+
# サーバー側でremember_digestをnilにしてるから
|
328
|
+
|
329
|
+
# nilに対して.is_password?とかやるとfor nil classエラーが起きちゃう
|
330
|
+
|
331
|
+
# remember_digestがnilの場合はfalseを返して、処理を止める
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# 後置if文に当てはまる条件があれば処理を止めて!
|
336
|
+
|
337
|
+
# って場合はreturnとかで明示的に書くとif ~ else ~ end
|
338
|
+
|
339
|
+
# とか書かなくて済む
|
340
|
+
|
341
|
+
BCrypt::Password.new(remember_digest).is_password?(remember_token)
|
342
|
+
|
343
|
+
end
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
# ユーザーのログイン情報を破棄する
|
348
|
+
|
349
|
+
def forget
|
350
|
+
|
351
|
+
update_attribute(:remember_digest,nil)
|
352
|
+
|
353
|
+
end
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
```
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
#### Drink.rb
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
|
369
|
+
class Drink < ApplicationRecord
|
370
|
+
|
371
|
+
belongs_to :user, dependent: :destroy
|
372
|
+
|
373
|
+
with_options presence: true do
|
374
|
+
|
375
|
+
validates :name
|
376
|
+
|
377
|
+
validates :explain
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
```
|
386
|
+
|
229
387
|
### 試したこと
|
230
388
|
|
231
389
|
|