I am getting a mystifying error when trying to access Questions#Show. Anytime I redirect to the View, I get a 500 internal server error. According to my server trace, the issue is in the Questions controller in '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:
However I am not quite sure what is wrong with it
Questions 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
Anyone familiar with this TypeError? I have tried Google and StackOverflow and haven't seen any hints I have used virtually identical code for other models, however this is the only one which gives me a TypeError
Stack Trace
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'
I succeeded in reproducing the same exception. Perhaps your code or the gem you're using is running a query with session.id
.
Solution
If your code uses session.id
to identify a user, you should convert it to a String before using it.
# 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!
If your code doesn't run a query by session.id
, you should be suspicious of gems that insert processing in the middle of a rack session.
Steps to debug
This code is running in 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
If you run a query by session.id
, you get the TypeError
.
SomethingModel.where(session_id: session.id).to_a
# => TypeError can't quote Rack::Session::SessionId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With