gpt4 book ai didi

ruby-on-rails - 类型错误 - 不能引用 Rack::Session::SessionId

转载 作者:行者123 更新时间:2023-12-04 14:45:27 25 4
gpt4 key购买 nike

我在尝试访问 Questions#Show 时遇到了一个令人费解的错误。每当我重定向到 View 时,都会收到 500 内部服务器错误。根据我的服务器跟踪,问题出在“set_question”中的问题 Controller 中

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:

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

问题 Controller
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 吗?我试过谷歌和 StackOverflow 并没有看到任何提示
我对其他模型使用了几乎相同的代码,但是这是唯一一个给我一个 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'

最佳答案

我成功地重现了相同的异常。也许您的代码或您使用的 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 运行查询,您应该怀疑在 Rack session 中间插入处理的 gem。

调试步骤

此代码在 Rails 5.2.4.2 中运行.
# 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

关于ruby-on-rails - 类型错误 - 不能引用 Rack::Session::SessionId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60019801/

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