#< Devise :: OmniauthCallbacksController:0x007fb5d1741e48>的未定义局部变量或方法"flash"

编程入门 行业动态 更新时间:2024-10-11 15:18:02
本文介绍了#< Devise :: OmniauthCallbacksController:0x007fb5d1741e48>的未定义局部变量或方法"flash"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Omniauth-facebook和Devise-token-auth以及前端的Angular和ng-token-auth构建Rails-API.但是,使用Facebook登录时,出现错误:

I am building a Rails-API using Omniauth-facebook and Devise-token-auth with Angular and ng-token-auth for the frontend. However when logging in with facebook I am presented with the error:

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

似乎omniauth会自动使用Flash中间件,但是rails-api不包含该中间件,并且我一直未能成功禁用omniauth的Flash使用.我的配置如下:

It seems omniauth automatically uses flash middleware however the rails-api doesn't include this and I have been unsuccessfully disabling the use of flash with omniauth. My configuration is as below:

application.rb:

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

devise_token_auth.rb:

devise_token_auth.rb:

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

devise.rb:

devise.rb:

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

omniauth.rb:

omniauth.rb:

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

我无法通过以下方式禁用Flash错误:

I have not managed to disable the flash error with:

config.navigational_formats = [:json]

并且devise/omniauth仍在使用Flash中间件并抛出错误,感谢任何帮助!

and devise/omniauth is still using flash middleware and throws the error, any help appreciated!

推荐答案

出现相同的问题.在设计源代码中搜索"flash".找到约17个匹配项,全部使用 set_flash_message!(带有感叹号),但 OmniauthCallbacksController 中的 failure 方法除外,该方法使用set_flash_message (无感叹号).查看定义,我们看到:

Had the same problem. Searched the devise source code for 'flash'. Found about 17 matches, all using set_flash_message! (with exclamation mark), except for the failure method in the OmniauthCallbacksController, which uses set_flash_message (without exclamation mark). Looking at the definition we see:

\ 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

实际的闪断消息是在没有感叹号的方法中生成的(我本来会以其他方式处理它的...).缺少感叹号的原因是设置其他解决方案中提到的 navigational_formats 在这里不起作用的原因.

The actual flash message is generated in the method without exclamation mark (I would have progged it the other way around...). The missing exclamation mark is the reason why setting the navigational_formats as mentioned in other solutions doesn't work here.

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

修复:从 OmniauthCallbacksController 修补 failure 方法.在初始化程序中进行此操作,例如在

The fix: monkey-patch the failure method from the OmniauthCallbacksController. Do this in an initializer, for example in

\ 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

更多推荐

#&lt; Devise :: OmniauthCallbacksController:0x007fb5d1741e48&gt;的未定义局部变量

本文发布于:2023-10-27 23:42:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   局部   未定义   方法   Devise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!