質問編集履歴

3

タイトル変更

2019/04/14 13:32

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- devise ログイン時にエラーを解決したい
1
+ deviseでのユーザーログインで認証失敗の際エラーメッセージ表示させたい
test CHANGED
File without changes

2

誤字修正

2019/04/14 13:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ### deviseでのユーザーログインで認証失敗の際エラーメッセージを表示さ。
1
+ ### deviseでのユーザーログインで認証失敗の際エラーメッセージを表示させたい
2
2
 
3
3
 
4
4
 

1

ファイルの追加

2019/04/13 12:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -186,57 +186,191 @@
186
186
 
187
187
  vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
188
188
 
189
- vendor/bundle/ruby/2.5.0/gems/web-console-3.7.0/lib/web_console/middleware.rb:135:in `call_app'
190
-
191
- vendor/bundle/ruby/2.5.0/gems/web-console-3.7.0/lib/web_console/middleware.rb:30:in `block in call'
192
-
193
- vendor/bundle/ruby/2.5.0/gems/web-console-3.7.0/lib/web_console/middleware.rb:20:in `catch'
194
-
195
- vendor/bundle/ruby/2.5.0/gems/web-console-3.7.0/lib/web_console/middleware.rb:20:in `call'
196
-
197
- vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
198
-
199
- vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:38:in `call_app'
200
-
201
- vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:26:in `block in call'
202
-
203
- vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `block in tagged'
204
-
205
- vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:28:in `tagged'
206
-
207
- vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `tagged'
208
-
209
- vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/rack/logger.rb:26:in `call'
210
-
211
- vendor/bundle/ruby/2.5.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13:in `call'
212
-
213
- vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
214
-
215
- vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/request_id.rb:27:in `call'
216
-
217
- vendor/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/method_override.rb:22:in `call'
218
-
219
- vendor/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/runtime.rb:22:in `call'
220
-
221
- vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
222
-
223
- vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
224
-
225
- vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/static.rb:127:in `call'
226
-
227
- vendor/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/sendfile.rb:111:in `call'
228
-
229
- vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/engine.rb:524:in `call'
230
-
231
- vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
232
-
233
- vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
234
-
235
- vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
236
-
237
- vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
238
-
239
- vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
189
+ ```
190
+
191
+
192
+
193
+ sessions_controller.rb
194
+
195
+ ```
196
+
197
+ # frozen_string_literal: true
198
+
199
+
200
+
201
+ class Users::SessionsController < Devise::SessionsController
202
+
203
+ before_action :alert_account, only: :destroy, raise: false
204
+
205
+ before_action :reset_session_before_login, only: :create
206
+
207
+ protect_from_forgery :except => [:create]
208
+
209
+
210
+
211
+ # before_action :configure_sign_in_params, only: [:create]
212
+
213
+
214
+
215
+ # GET /resource/sign_in
216
+
217
+ # def new
218
+
219
+ # super
220
+
221
+ # end
222
+
223
+
224
+
225
+ # POST /resource/sign_in
226
+
227
+ # def create
228
+
229
+ # super
230
+
231
+ # end
232
+
233
+
234
+
235
+ # DELETE /resource/sign_out
236
+
237
+ # def destroy
238
+
239
+ # super
240
+
241
+ # end
242
+
243
+
244
+
245
+ # protected
246
+
247
+
248
+
249
+ # If you have extra params to permit, append them to the sanitizer.
250
+
251
+ # def configure_sign_in_params
252
+
253
+ # devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
254
+
255
+ # end
256
+
257
+ private
258
+
259
+
260
+
261
+ def reset_session_before_login
262
+
263
+ user_return_to = session[:user_return_to]
264
+
265
+ reset_session
266
+
267
+ session[:user_return_to] = user_return_to if user_return_to
268
+
269
+ end
270
+
271
+ end
272
+
273
+ ```
274
+
275
+
276
+
277
+ application_controller.rb
278
+
279
+
280
+
281
+ ```
282
+
283
+ class ApplicationController < ActionController::Base
284
+
285
+ protect_from_forgery with: :exception
286
+
287
+ before_action :configure_permitted_parameters, if: :devise_controller?
288
+
289
+ before_action :configure_account_update_params, if: :devise_controller?
290
+
291
+ before_action :set_host
292
+
293
+ before_action :store_current_location, unless: :devise_controller?
294
+
295
+
296
+
297
+ def alert_account
298
+
299
+ unless current_user
300
+
301
+ flash[:alert] = "アカウントの作成もしくはログインしてください"
302
+
303
+ redirect_to new_user_session_path
304
+
305
+ end
306
+
307
+ end
308
+
309
+
310
+
311
+ def configure_permitted_parameters
312
+
313
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :tall, :age, :gendar, :active_level, :recom_calorie, :image, :password, :password_confirmation, :email])
314
+
315
+ end
316
+
317
+
318
+
319
+ def configure_account_update_params
320
+
321
+ devise_parameter_sanitizer.permit(:account_update, keys: [:username,:tall, :age, :gendar, :active_level, :recom_calorie, :image])
322
+
323
+ end
324
+
325
+
326
+
327
+ def require_admin
328
+
329
+ redirect_to root_path unless current_user.admin?
330
+
331
+ end
332
+
333
+
334
+
335
+ def set_host
336
+
337
+ Rails.application.routes.default_url_options[:host] = request.host_with_port
338
+
339
+ end
340
+
341
+
342
+
343
+ def user_recom?
344
+
345
+ unless (current_user&.recom_calorie) || session[:recom_calorie]
346
+
347
+ flash[:notice] = "先に推奨カロリーを計算しましょう。"
348
+
349
+ unless current_user
350
+
351
+ redirect_to new_scalcalo_path
352
+
353
+ else
354
+
355
+ redirect_to edit_calcalo_path(current_user)
356
+
357
+ end
358
+
359
+ end
360
+
361
+ end
362
+
363
+
364
+
365
+ def store_current_location
366
+
367
+ return if current_user
368
+
369
+ store_location_for(:user, request.url)
370
+
371
+ end
372
+
373
+ end
240
374
 
241
375
  ```
242
376