TypeError - 无法引用 Rack::Session::SessionId

TypeError - can't quote Rack::Session::SessionId

我在尝试访问 Questions#Show 时遇到了一个神秘的错误。每当我重定向到视图时,都会收到 500 内部服务器错误。根据我的服务器跟踪,问题出在 'set_question'

中的问题控制器中
Started GET "/questions/1" for 127.0.0.1 at 2020-02-01 18:57:34 +0100
Processing by QuestionsController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (1.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Question Load (3.5ms)  SELECT `questions`.* FROM `questions` WHERE `questions`.`id` = 1 LIMIT 1
  ↳ app/controllers/questions_controller.rb:105:in `set_question'
Completed 500 Internal Server Error in 15ms (ActiveRecord: 4.6ms | Allocations: 3861)

TypeError - can't quote Rack::Session::SessionId:

但是我不太确定它有什么问题

问题控制器

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]
  impressionist :actions => [:show], unique: [:session_hash]

  def index
  end

  def show
    @answers = @question.answers.includes(:user).page(params[:page]).per(5)
  end

  def new
    @question = Question.new
  end

  def edit
    authorize @question
  end

  def create
    @question = Question.new(question_params)

    respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'You have successfully asked a question!' }
        format.json { render :show, status: :created, location: @question }
      else
        format.html { render :new }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    authorize @question
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question successfully updated.' }
        format.json { render :show, status: :ok, location: @question }
      else
        format.html { render :edit }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize @question
    @question.destroy
    respond_to do |format|
      format.html { redirect_to @questions_url, notice: 'Question successfully deleted.' }
      format.json { head :no_content }
    end
  end

  private

  def set_question
    @question = Question.find(params[:id])
  end

  def question_params
    params.require(:question).permit(:title, :body, :tag_list).merge(user_id: current_user.id)
  end
end

有人熟悉这个 TypeError 吗?我试过 Google 和 Whosebug,但没有看到任何提示 我对其他模型使用了几乎相同的代码,但这是唯一给我 TypeError

的代码

堆栈跟踪

activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/quoting.rb:231:in `_quote'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/quoting.rb:18:in `quote'
activerecord (6.0.2.1) lib/arel/collectors/substitute_binds.rb:17:in `add_bind'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:703:in `visit_Arel_Nodes_BindParam'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:611:in `visit_Arel_Nodes_Equality'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:790:in `block in inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each_with_index'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:578:in `visit_Arel_Nodes_And'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:790:in `block in inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each_with_index'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:181:in `collect_nodes_for'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:161:in `visit_Arel_Nodes_SelectCore'
activerecord (6.0.2.1) lib/arel/visitors/mysql.rb:28:in `visit_Arel_Nodes_SelectCore'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:128:in `block in visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `inject'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/mysql.rb:23:in `visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:11:in `accept'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:18:in `compile'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb:34:in `to_sql_and_binds'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/query_cache.rb:99:in `select_all'
activerecord (6.0.2.1) lib/active_record/connection_adapters/mysql/database_statements.rb:12:in `select_all'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb:77:in `select_one'
activerecord (6.0.2.1) lib/active_record/relation/finder_methods.rb:317:in `block in exists?'
activerecord (6.0.2.1) lib/active_record/relation.rb:828:in `skip_query_cache_if_necessary'
activerecord (6.0.2.1) lib/active_record/relation/finder_methods.rb:317:in `exists?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:103:in `check_unique_impression?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:99:in `check_impression?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:94:in `unique?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:44:in `impressionist_subapp_filter'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:7:in `block in impressionist'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:429:in `instance_exec'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:429:in `block in make_lambda'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:201:in `block (2 levels) in halting'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:202:in `block in halting'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `block in invoke_before'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `each'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `invoke_before'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:110:in `block in run_callbacks'
public_activity (1.6.4) lib/public_activity/utility/store_controller.rb:27:in `store_controller_for_public_activity'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:139:in `run_callbacks'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.2.1) lib/action_view/rendering.rb:39:in `process'
rack-mini-profiler (1.1.5) lib/mini_profiler/profiling_methods.rb:104:in `block in profile_method'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
rack-pjax (1.1.0) lib/rack/pjax.rb:12:in `call'
bullet (6.1.0) lib/bullet/rack.rb:15:in `call'
remotipart (1.4.4) lib/remotipart/middleware.rb:32:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.1.2) lib/rack/tempfile_reaper.rb:17:in `call'
rack (2.1.2) lib/rack/etag.rb:27:in `call'
rack (2.1.2) lib/rack/conditional_get.rb:27:in `call'
rack (2.1.2) lib/rack/head.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.1.2) lib/rack/session/abstract/id.rb:269:in `context'
rack (2.1.2) lib/rack/session/abstract/id.rb:263:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.2.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.1.2) lib/rack/method_override.rb:24:in `call'
rack (2.1.2) lib/rack/runtime.rb:24:in `call'
activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.1.2) lib/rack/sendfile.rb:113:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
rack-mini-profiler (1.1.5) lib/mini_profiler/profiler.rb:296:in `call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread' 

显然,您的 set_question 方法没有任何问题。错误显示:TypeError - can't quote Rack::Session::SessionId: 并且发生在 impressionist :actions => [:show], unique: [:session_hash].

我没用过这个 gem 所以不能确切地告诉你如何解决这个错误,但为了证明我的观点,如果你在你的控制器中评论这一行(第 3 行),它会起作用.

您需要阅读更多有关实施此问题的信息gem来解决它。

我成功重现了同样的异常。也许您的代码或您正在使用的 gem 运行 使用 session.id.

进行查询

解决方案

如果您的代码使用 session.id 来标识用户,您应该在使用前将其转换为字符串。

# Bad
user = User.find_by(session_id: session.id)
# => TypeError can't quote Rack::Session::SessionId

# Good
user = User.find_by(session_id: session.id.to_s)
# Success!

如果您的代码没有 运行 session.id 的查询,您应该怀疑 gem 在机架会话中间插入处理的程序。

调试步骤

此代码在 Rails 5.2.4.2 中 运行ning。

# In controller
def new
  logger.info "class=#{session.id.class} to_s=#{session.id.to_s}"
end

# The log for the above code
# INFO -- class=Rack::Session::SessionId to_s=adf8...9af0 <- a long string

如果您 运行 通过 session.id 查询,您会得到 TypeError

SomethingModel.where(session_id: session.id).to_a
# => TypeError can't quote Rack::Session::SessionId

替换config/initializers/session_store.rb中的以下代码:

Rails.application.config.session_store :active_record_store, :key => '....'

Rails.application.config.action_dispatch.session_store :active_record_store, :key => '....'

为我修复了 TypeError can't quote Rack::Session::SessionId 问题。

注意:确保用您自己的密钥替换“....”。

会话哈希在 impressionist 中是字符串,但如果我们使用设备,我们的会话将与 id 一起存储。不确定这个印象派 gem 但以下解决了我的问题:

impressionist :actions => [:show], unique: [:impressionable_type,:impressionable_id,:session_hash.to_s]