ruby-on-rails 未定义#的局部变量或方法'flash'< Devise::OmniauthCallbacksController:0x007fb5d1741e48>

xvw2m8pv  于 2022-12-24  发布在  Ruby
关注(0)|答案(3)|浏览(160)

我正在使用Omniauth-facebook和Devise-token-auth构建一个Rails-API,前端使用Angular和ng-token-auth,但是当我使用facebook登录时,出现了错误:

undefined local variable or method `flash' for #<Devise::OmniauthCallbacksController:0x007fd027a51e10>

看起来omniauth自动使用flash中间件,但是rails-api没有包含这个,而且我一直没有成功地禁用omniauth使用flash。我的配置如下:
application.rb:

require File.expand_path('../boot', __FILE__)

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module PathfinderApi
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

    config.api_only = true
    config.middleware.use ActionDispatch::Flash
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore
  end
end

设备_令牌_授权.rb:

DeviseTokenAuth.setup do |config|
  Rails.application.secrets.facebook_app_secret
  config.change_headers_on_each_request = true
end

devise.rb:

Devise.setup do |config|
  config.navigational_formats = [:json]
end

omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook,      ENV['APP_KEY'], ENV['APP_SECRET']
end

我无法使用以下命令禁用闪存错误:

config.navigational_formats = [:json]

并且devise/omniauth仍然在使用flash中间件并抛出错误,任何帮助都很感激!

qni6mghb

qni6mghb1#

遇到了同样的问题。搜索了device源代码中的"flash"。找到了大约17个匹配项,都使用set_flash_message!(带感叹号),除了OmniauthCallbacksController中的failure方法,它使用set_flash_message(不带感叹号)。查看定义,我们可以看到:

  • \应用程序\控制器\设备\omniauth回调控制器. rb *
# Sets flash message if is_flashing_format? equals true
def set_flash_message!(key, kind, options = {})
  if is_flashing_format?
    set_flash_message(key, kind, options)
  end
end
  • \库\设备\控制器\助手. rb *
def is_flashing_format?
  is_navigational_format?
end

def is_navigational_format?
  Devise.navigational_formats.include?(request_format)
end

实际的flash消息是在没有感叹号的方法中生成的(我会用相反的方式来探测它...)。缺少感叹号是其他解决方案中提到的设置navigational_formats在这里不起作用的原因。

    • 结论:他们忘记了鼓掌标志。
    • 修复**:monkey-patch OmniauthCallbacksController中的failure方法。在初始化程序中执行此操作,例如在
  • \配置\初始化程序\设备. rb *
Rails.application.config.to_prepare do              # to_prepare ensures that the monkey patching happens before the first request
  Devise::OmniauthCallbacksController.class_eval do # reopen the class
    def failure                                     # redefine the failure method
      set_flash_message! :alert, :failure, kind: OmniAuth::Utils.camelize(failed_strategy.name), reason: failure_message
      redirect_to after_omniauth_failure_path_for(resource_name)
    end
  end
end
eivnm1vs

eivnm1vs2#

在Rails(5.0.0.1)+设计令牌授权(0.1.39)中也有同样的问题。
除了@Koen的回答中的覆盖之外,在我的例子中还需要添加以下内容:

# in config/application.rb
  config.middleware.use ActionDispatch::Cookies
huwehgph

huwehgph3#

我设法解决这个问题,添加到device.rb配置:
第一个月
这样设计就不会尝试使用闪存,也不会抛出500错误。

相关问题