首页 > 解决方案 > 为什么“respond_with”不执行失败的设计认证?

问题描述

成功验证后,我可以这样覆盖respond_with

class SessionsController < Devise::SessionsController

  def respond_with(resource, *)
    if user_signed_in?
      redirect_to root_path
    end
  end

end

但是,当身份验证失败时——假设密码不正确——我在设计中respond_with的覆盖SessionsController不会被调用。我知道,对于失败的身份验证, Devise 然后调用Devise::SessionsController#new。但是,在该DeviseController#new方法结束时respond_with被调用。

所以我的问题是:

1) 为什么在Devise::SessionsController#create中认证成功时respond_with我的覆盖成功执行?SessionsController

2) 为什么我在身份验证失败时对我的覆盖没有成功respond_with执行?控制器操作以相同的方法完成SessionsControllerDevise::SessionsController#createDevise::SessionsController#newrespond_withDevise::SessionsController#create

这是我的宝石文件:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.5'

gem 'rails', '~> 6.0.1'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '5.1.1'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'haml', '~> 5.0', '>= 5.0.4'
gem 'pg', '1.2.3'
gem 'rtesseract', '3.1'
gem 'sidekiq', '6.0.6'
gem 'dotenv-rails', '2.7.5'
gem 'devise', '4.7.1'
gem 'letter_opener', '1.7.0'

group :production do
  gem 'aws-sdk-s3', '1.61.2'
end

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'awesome_print'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

标签: ruby-on-railsdevise

解决方案


推荐阅读