gpt4 book ai didi

ruby-on-rails - # 的未定义局部变量或方法 `flash'

转载 作者:行者123 更新时间:2023-12-04 05:58:23 25 4
gpt4 key购买 nike

我正在使用 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。
我的配置如下:

应用程序.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

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

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

omn​​iauth.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 中间件并抛出错误,任何帮助表示赞赏!

最佳答案

有同样的问题。在设计源代码中搜索“flash”。找到大约 17 个匹配项,全部使用 set_flash_message! (带感叹号),除了 failure OmniauthCallbacksController 中的方法, 使用 set_flash_message (没有感叹号)。查看定义,我们看到:

\app\controllers\devise\omniauth_callbacks_controller.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

\lib\devise\controllers\helpers.rb
def is_flashing_format?
is_navigational_format?
end

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

实际的 flash 消息是在没有感叹号的方法中生成的(我本来可以反过来编写它......)。缺少感叹号是设置 navigational_formats的原因正如其他解决方案中提到的那样在这里不起作用。

结论:他们忘记了感叹号。

修复 :猴子补丁 failure方法来自 OmniauthCallbacksController .在初始化程序中执行此操作,例如在

\config\initializers\devise.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

关于ruby-on-rails - #<Devise::OmniauthCallbacksController:0x007fb5d1741e48> 的未定义局部变量或方法 `flash',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com