gpt4 book ai didi

ruby-on-rails - Rails 5 API 和 devise flash 未定义方法错误

转载 作者:行者123 更新时间:2023-12-01 22:22:30 24 4
gpt4 key购买 nike

我正在尝试在此 new rails 5 API application 上设置设计.当我尝试登录时(通过 POST 请求向 /v1/sign_in 发送正确的电子邮件和密码)我收到以下错误:

NameError (undefined local variable or method `flash' for #<SessionsController:0x007fa0f14fd280>):

devise (4.2.0) app/controllers/devise_controller.rb:157:in `set_flash_message'
devise (4.2.0) app/controllers/devise_controller.rb:164:in `set_flash_message!'
devise (4.2.0) app/controllers/devise/sessions_controller.rb:18:in `create'
actionpack (5.0.0) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:126:in `process'
actionpack (5.0.0) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.0.0) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:725:in `call'
bullet (5.2.0) lib/bullet/rack.rb:12:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
activerecord (5.0.0) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `call'
request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

我尝试了一些方法来修复它,比如在此处添加 flash 中间件:https://github.com/johndel/rails5-api-test/blob/master/config/application.rb#L44。我还尝试使用以下代码在开头添加它:config.middleware.insert_before(Rack::Sendfile, ActionDispatch::Flash) 但仍然没有成功。知道为什么这不起作用吗?

如果您设置了上述应用程序,您可以重现它,为它播种并尝试运行此命令:curl -X POST -F "user[email]=istoselidas@gmail.com"-F "user[password]=example"localhost:3000/v1/sign_in

更新似乎 sign_in.json 开箱即用,但是 password/edit.json 有问题,这似乎是设计问题(它接近它在这个 Controller 上有所不同)。

最佳答案

如果你真的想保留Flash,

添加中间件确实是必要的。您还需要在 SessionsController 中包含 Flash 模块。模块 Flash 显然隐式依赖于 Helpers,因此也包含它。

class SessionsController < Devise::SessionsController
include ActionController::Helpers
include ActionController::Flash
...

请注意,Devise 让它的 Controller 继承自您的 ApplicationController,而后者又继承自 ActionController::API,这比通常的父类 更 slim ActionController::Base.


但是,您不必

Devise 实际上知道所谓的导航格式。通常,这只是 :html(如果未指定其他格式,这也恰好是请求的默认格式)。

如果您在请求中明确使用 :json 格式,设计将很聪明,不会添加闪光灯。

为此,您必须撤消所做的配置:您将 :json 声明为导航格式。不。在您的设计初始值设定项中还原该行:

config.navigational_formats = [:json]  # THIS IS BAD

然后,在您的请求中使用额外的 .json 后缀,一切都会按预期工作:

curl -X POST -F "user[email]=istoselidas@gmail.com" -F "user[password]=example" localhost:3000/v1/sign_in.json

您也可以摆脱 Flash 中间件。


扩展问题的扩展答案:password/edit.json

似乎 Devise 的 PasswordsController 在设置 flash 消息时尊重非导航格式。到目前为止,这可能已经足够好了,因为没有人关心使用 JSON API 的 newedit 操作。您在规范中确保这些操作不可访问,这是完全合法的。

据我了解,这是 Devise 中的一个错误。让我们设计一个拉取请求来修复它! :)

您的 PasswordsController 通过覆盖 newedit 操作来专门化 Devise::PasswordsController。您仍然收到错误的原因是有害代码驻留在名为 assert_reset_token_passedbefore_filter 中。

因此,一个快速而干净的解决方案是添加您自己的过滤器。您可以使用 json_not_found 方法。由于该方法渲染了一些东西,它将阻止 Devise 的过滤器被执行。使用这种方法,您也不再需要重写操作方法。

class PasswordsController < Devise::PasswordsController
prepend_before_filter :json_not_found, only: [:new, :edit]
end

关于ruby-on-rails - Rails 5 API 和 devise flash 未定义方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119168/

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