質問編集履歴
1
loginのviewとparamsの中身を記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -181,3 +181,151 @@
|
|
181
181
|
gem 'bcrypt', '3.1.12'
|
182
182
|
|
183
183
|
```
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
__sessions/new.html.erb__
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
|
191
|
+
<% provide(:title, "Log in") %>
|
192
|
+
|
193
|
+
<h1>Log in</h1>
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
<div class="row">
|
198
|
+
|
199
|
+
<div class="col-md-6 col-md-offset-3">
|
200
|
+
|
201
|
+
<%= form_with url: login_path do |f| %>
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
<%= f.label :email %>
|
206
|
+
|
207
|
+
<%= f.email_field :email, class: 'form-control' %>
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
<%= f.label :password %>
|
212
|
+
|
213
|
+
<%= f.password_field :password, class: 'form-control' %>
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
<%= f.submit "Log in", class: "btn btn-primary" %>
|
218
|
+
|
219
|
+
<% end %>
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
|
224
|
+
|
225
|
+
</div>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
Railsチュートリアルの教材と変更した点は、**form_for→form_with**
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
paramsメソッドは、sessions_controllerには記載していません。
|
236
|
+
|
237
|
+
__users_controller__で記載している
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
|
241
|
+
class UsersController < ApplicationController
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
def show
|
246
|
+
|
247
|
+
#(params[:id])にユーザーidの1が入る
|
248
|
+
|
249
|
+
@user = User.find(params[:id])
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def new
|
256
|
+
|
257
|
+
#空のオブジェクト作成
|
258
|
+
|
259
|
+
@user = User.new
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def create
|
266
|
+
|
267
|
+
@user = User.new(user_params)
|
268
|
+
|
269
|
+
if @user.save
|
270
|
+
|
271
|
+
log_in @user
|
272
|
+
|
273
|
+
flash[:success] = "Welcome to the Sample App!"
|
274
|
+
|
275
|
+
redirect_to @user
|
276
|
+
|
277
|
+
else
|
278
|
+
|
279
|
+
render 'new'
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
private
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
def user_params
|
292
|
+
|
293
|
+
params.require(:user).permit(:name,:email,:password,:password_confirmaiton)
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
```
|
300
|
+
|
301
|
+
paramsの中身をdebuggerで検証。
|
302
|
+
|
303
|
+
```
|
304
|
+
|
305
|
+
2, 11] in /Users/nishiwakidaiki/Desktop/sample_app/app/controllers/sessions_controller.rb
|
306
|
+
|
307
|
+
2: def new
|
308
|
+
|
309
|
+
3: end
|
310
|
+
|
311
|
+
4:
|
312
|
+
|
313
|
+
5: def create
|
314
|
+
|
315
|
+
6: debugger
|
316
|
+
|
317
|
+
=> 7: user = User.find_by(email: params[:session][:email].downcase)
|
318
|
+
|
319
|
+
8: if user && user.authenticate(params[:session][:password])
|
320
|
+
|
321
|
+
9: # ユーザーログイン後にユーザー情報のページにリダイレクトする
|
322
|
+
|
323
|
+
10: log_in user
|
324
|
+
|
325
|
+
11: redirect_to user
|
326
|
+
|
327
|
+
(byebug) params
|
328
|
+
|
329
|
+
#<ActionController::Parameters {"authenticity_token"=>"fMkUiCsYcuYyMSedk1n9yPrEvvqLVx4Fgsp36NCLrLl8Ie3_LFZLKzY-Q-i3VqV8BD0bfjVGe4VnuUv7JAMhzw", "email"=>"", "password"=>"", "commit"=>"Log in", "controller"=>"sessions", "action"=>"create"} permitted: false>
|
330
|
+
|
331
|
+
```
|