使用 Devise 从 Turbolinks 升级到 Turbo 后,我得到了 "undefined method `to_model' for true:TrueClass"
I got "undefined method `to_model' for true:TrueClass" after upgrade from Turbolinks to Turbo with Devise
我有这个控制器
# app/controllers/users/sessions_controller
class Users::SessionsController < Devise::SessionsController
before_action :redirect_admin_user, only: [:new]
skip_before_action :verify_authenticity_token, only: [:create]
def destroy
super
end
protected
def redirect_admin_user
if admin_signed_in?
redirect_to root_url, alert: "Please sign out of admin session"
end
end
end
我尝试登录并收到错误 "NoMethodError in Users::SessionsController#create" "undefined method `to_model ' 对于 true:TrueClass" at I18n.with_locale(locale, &action) from this controller
# app/controllers/application_controller
require 'action_text'
class ApplicationController < ActionController::Base
Turbo::Redirection
include Pagy::Backend
protect_from_forgery with: :exception
around_action :switch_locale
before_action :configure_permitted_parameters, if: :devise_controller?
after_action :track_action
def switch_locale(&action)
if user_signed_in?
locale = current_user.language
I18n.with_locale(locale, &action)
else
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
end
protected
def track_action
...
end
def set_locale
...
end
def configure_permitted_parameters
...
end
def after_sign_in_path_for(resource)
dashboard_path
ahoy.authenticate(resource)
end
def current_user?(user)
...
end
def admin_signed_in?
...
end
def authenticate_admin!
...
end
helper_method :current_user?
helper_method :admin_signed_in?
end
应用程序跟踪
app/controllers/application_controller.rb:18:in `switch_locale'
完整跟踪
actionpack (6.1.4) lib/action_dispatch/routing/polymorphic_routes.rb:264:in `handle_model'
actionpack (6.1.4) lib/action_dispatch/routing/polymorphic_routes.rb:279:in `handle_model_call'
actionpack (6.1.4) lib/action_dispatch/routing/url_for.rb:192:in `full_url_for'
actionpack (6.1.4) lib/action_dispatch/routing/url_for.rb:171:in `url_for'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:122:in `_process_options'
actionpack (6.1.4) lib/action_controller/metal/streaming.rb:201:in `_process_options'
actionview (6.1.4) lib/action_view/rendering.rb:102:in `render_to_body'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.1.4) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.1.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
/Users/pui/.rbenv/versions/3.0.2/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
activesupport (6.1.4) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:86:in `cleanup_view_runtime'
searchkick (4.5.2) lib/searchkick/logging.rb:217:in `cleanup_view_runtime'
activerecord (6.1.4) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:45:in `render'
responders (3.0.1) lib/action_controller/responder.rb:240:in `default_render'
responders (3.0.1) lib/action_controller/responder.rb:172:in `to_html'
responders (3.0.1) lib/action_controller/responder.rb:165:in `respond'
responders (3.0.1) lib/action_controller/responder.rb:158:in `call'
responders (3.0.1) lib/action_controller/respond_with.rb:213:in `respond_with'
devise (4.8.0) app/controllers/devise/sessions_controller.rb:23:in `create'
actionpack (6.1.4) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.1.4) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.1.4) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.1.4) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
i18n (1.8.10) lib/i18n.rb:314:in `with_locale'
app/controllers/application_controller.rb:18:in `switch_locale'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
react-rails (2.6.1) lib/react/rails/controller_lifecycle.rb:31:in `use_react_component_helper'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
ahoy_matey (3.2.0) lib/ahoy/controller.rb:45:in `set_ahoy_request_store'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
actiontext (6.1.4) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (6.1.4) lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `instance_exec'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
activesupport (6.1.4) lib/active_support/callbacks.rb:137:in `run_callbacks'
actionpack (6.1.4) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (6.1.4) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.4) lib/active_support/notifications.rb:203:in `instrument'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
searchkick (4.5.2) lib/searchkick/logging.rb:212:in `process_action'
activerecord (6.1.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.1.4) lib/abstract_controller/base.rb:165:in `process'
actionview (6.1.4) lib/action_view/rendering.rb:39:in `process'
actionpack (6.1.4) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.1.4) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.1.4) lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
actionpack (6.1.4) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:842:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
rack (2.2.3) lib/rack/deflater.rb:44:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.1.4) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (6.1.4) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (6.1.4) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.4) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.4) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.4) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.4) lib/rails/rack/logger.rb:26:in `call'
ahoy_matey (3.2.0) lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
actionpack (6.1.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/host_authorization.rb:92:in `call'
webpacker (6.0.0.rc.2) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.7.0) lib/rack/proxy.rb:63:in `call'
railties (6.1.4) lib/rails/engine.rb:539:in `call'
puma (5.2.2) lib/puma/configuration.rb:248:in `call'
puma (5.2.2) lib/puma/request.rb:76:in `block in handle_request'
puma (5.2.2) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
puma (5.2.2) lib/puma/request.rb:75:in `handle_request'
puma (5.2.2) lib/puma/server.rb:431:in `process_client'
puma (5.2.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
资源:
ruby '3.0.2'
rails '~> 6.1', '>= 6.1.4'
rails-i18n '~> 6.0'
设计 '~> 4.7', '>= 4.8'
我通过 coding 在 rails 上学习了 ruby 任何人都可以告诉我如何解决这个错误吗?
问题就在这里:
def after_sign_in_path_for(resource)
dashboard_path
ahoy.authenticate(resource)
end
此方法应该返回一个字符串到您应该重定向用户的位置,ahoy.authenticate
而不是 returns 一个布尔值。利用 Devise 控制器的流程的一种不那么骇人听闻的方法是简单地调用 super
块:
# @see https://github.com/rubocop/ruby-style-guide#namespace-definition
module Users
class SessionsController < Devise::SessionsController
before_action :redirect_admin_user, only: [:new]
skip_before_action :verify_authenticity_token, only: [:create]
# note that your destroy method is completely superflouos - just use the super method
def create
# the parent controller yields after successfully signing a user in
super do |resource|
ahoy.authenticate(resource)
end
end
protected
def redirect_admin_user
if admin_signed_in?
redirect_to root_url, alert: "Please sign out of admin session"
end
end
# remove the method in ApplicationController
def after_sign_in_path_for(resource)
dashboard_path
end
end
end
end
这是可行的,因为几乎所有 Devise 控制器都在战略点屈服:
class Devise::SessionsController < DeviseController
# ....
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message!(:notice, :signed_in)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
# ...
end
它不是很明显,但如果电子邮件或密码无效,sign_in
将抛出异常,因此只有在用户身份验证成功后才能到达 yield resource if block_given?
。
我有这个控制器
# app/controllers/users/sessions_controller
class Users::SessionsController < Devise::SessionsController
before_action :redirect_admin_user, only: [:new]
skip_before_action :verify_authenticity_token, only: [:create]
def destroy
super
end
protected
def redirect_admin_user
if admin_signed_in?
redirect_to root_url, alert: "Please sign out of admin session"
end
end
end
我尝试登录并收到错误 "NoMethodError in Users::SessionsController#create" "undefined method `to_model ' 对于 true:TrueClass" at I18n.with_locale(locale, &action) from this controller
# app/controllers/application_controller
require 'action_text'
class ApplicationController < ActionController::Base
Turbo::Redirection
include Pagy::Backend
protect_from_forgery with: :exception
around_action :switch_locale
before_action :configure_permitted_parameters, if: :devise_controller?
after_action :track_action
def switch_locale(&action)
if user_signed_in?
locale = current_user.language
I18n.with_locale(locale, &action)
else
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
end
protected
def track_action
...
end
def set_locale
...
end
def configure_permitted_parameters
...
end
def after_sign_in_path_for(resource)
dashboard_path
ahoy.authenticate(resource)
end
def current_user?(user)
...
end
def admin_signed_in?
...
end
def authenticate_admin!
...
end
helper_method :current_user?
helper_method :admin_signed_in?
end
应用程序跟踪
app/controllers/application_controller.rb:18:in `switch_locale'
完整跟踪
actionpack (6.1.4) lib/action_dispatch/routing/polymorphic_routes.rb:264:in `handle_model'
actionpack (6.1.4) lib/action_dispatch/routing/polymorphic_routes.rb:279:in `handle_model_call'
actionpack (6.1.4) lib/action_dispatch/routing/url_for.rb:192:in `full_url_for'
actionpack (6.1.4) lib/action_dispatch/routing/url_for.rb:171:in `url_for'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:122:in `_process_options'
actionpack (6.1.4) lib/action_controller/metal/streaming.rb:201:in `_process_options'
actionview (6.1.4) lib/action_view/rendering.rb:102:in `render_to_body'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.1.4) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.1.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
/Users/pui/.rbenv/versions/3.0.2/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
activesupport (6.1.4) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:86:in `cleanup_view_runtime'
searchkick (4.5.2) lib/searchkick/logging.rb:217:in `cleanup_view_runtime'
activerecord (6.1.4) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:45:in `render'
responders (3.0.1) lib/action_controller/responder.rb:240:in `default_render'
responders (3.0.1) lib/action_controller/responder.rb:172:in `to_html'
responders (3.0.1) lib/action_controller/responder.rb:165:in `respond'
responders (3.0.1) lib/action_controller/responder.rb:158:in `call'
responders (3.0.1) lib/action_controller/respond_with.rb:213:in `respond_with'
devise (4.8.0) app/controllers/devise/sessions_controller.rb:23:in `create'
actionpack (6.1.4) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.1.4) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.1.4) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.1.4) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
i18n (1.8.10) lib/i18n.rb:314:in `with_locale'
app/controllers/application_controller.rb:18:in `switch_locale'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
react-rails (2.6.1) lib/react/rails/controller_lifecycle.rb:31:in `use_react_component_helper'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
ahoy_matey (3.2.0) lib/ahoy/controller.rb:45:in `set_ahoy_request_store'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
actiontext (6.1.4) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (6.1.4) lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `instance_exec'
activesupport (6.1.4) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
activesupport (6.1.4) lib/active_support/callbacks.rb:137:in `run_callbacks'
actionpack (6.1.4) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (6.1.4) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.4) lib/active_support/notifications.rb:203:in `instrument'
actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (6.1.4) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
searchkick (4.5.2) lib/searchkick/logging.rb:212:in `process_action'
activerecord (6.1.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.1.4) lib/abstract_controller/base.rb:165:in `process'
actionview (6.1.4) lib/action_view/rendering.rb:39:in `process'
actionpack (6.1.4) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.1.4) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.1.4) lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
actionpack (6.1.4) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:842:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.1) lib/omniauth/strategy.rb:169:in `call'
rack (2.2.3) lib/rack/deflater.rb:44:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
rack-tracker (1.12.1) lib/rack/tracker.rb:45:in `_call'
rack-tracker (1.12.1) lib/rack/tracker.rb:41:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.1.4) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (6.1.4) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (6.1.4) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.4) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.4) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.4) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.4) lib/rails/rack/logger.rb:26:in `call'
ahoy_matey (3.2.0) lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
actionpack (6.1.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.4) lib/action_dispatch/middleware/host_authorization.rb:92:in `call'
webpacker (6.0.0.rc.2) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.7.0) lib/rack/proxy.rb:63:in `call'
railties (6.1.4) lib/rails/engine.rb:539:in `call'
puma (5.2.2) lib/puma/configuration.rb:248:in `call'
puma (5.2.2) lib/puma/request.rb:76:in `block in handle_request'
puma (5.2.2) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
puma (5.2.2) lib/puma/request.rb:75:in `handle_request'
puma (5.2.2) lib/puma/server.rb:431:in `process_client'
puma (5.2.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
资源:
ruby '3.0.2'
rails '~> 6.1', '>= 6.1.4'
rails-i18n '~> 6.0'
设计 '~> 4.7', '>= 4.8'
我通过 coding 在 rails 上学习了 ruby 任何人都可以告诉我如何解决这个错误吗?
问题就在这里:
def after_sign_in_path_for(resource)
dashboard_path
ahoy.authenticate(resource)
end
此方法应该返回一个字符串到您应该重定向用户的位置,ahoy.authenticate
而不是 returns 一个布尔值。利用 Devise 控制器的流程的一种不那么骇人听闻的方法是简单地调用 super
块:
# @see https://github.com/rubocop/ruby-style-guide#namespace-definition
module Users
class SessionsController < Devise::SessionsController
before_action :redirect_admin_user, only: [:new]
skip_before_action :verify_authenticity_token, only: [:create]
# note that your destroy method is completely superflouos - just use the super method
def create
# the parent controller yields after successfully signing a user in
super do |resource|
ahoy.authenticate(resource)
end
end
protected
def redirect_admin_user
if admin_signed_in?
redirect_to root_url, alert: "Please sign out of admin session"
end
end
# remove the method in ApplicationController
def after_sign_in_path_for(resource)
dashboard_path
end
end
end
end
这是可行的,因为几乎所有 Devise 控制器都在战略点屈服:
class Devise::SessionsController < DeviseController
# ....
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message!(:notice, :signed_in)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
# ...
end
它不是很明显,但如果电子邮件或密码无效,sign_in
将抛出异常,因此只有在用户身份验证成功后才能到达 yield resource if block_given?
。