ruby-on-rails ActiveRecord::在活动存储重定向时未找到记录

42fyovps  于 2022-12-15  发布在  Ruby
关注(0)|答案(2)|浏览(102)

我的应用程序使用活动存储来管理图像(附加到文章、用户头像等),在处理以下样式的链接时,我会不断收到500个错误:

/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBODYwR1E9PSIsImV4cCI6bnVsbCwicHVyIjoiYmxvYl9pZCJ9fQ==--ac33ecf9f5dffc2b8345b443c402562e4fb6afaf/F582BAA7-DD39-4A7A-8E82-919EC8AFC12C.jpeg

这些链接有时会被搜索引擎机器人访问,有时是因为其他网站指向我的应用程序的图像,而这些图像已经不存在了。
为了生成指向图像的链接,我使用了Rails推荐的helper,它通过重定向指向资产(例如url_for(element.attachment))。
问题在于,如果资产不再存在,例如因为用户已经改变了化身,或者因为在文章中图像已经被替换,则这些链接生成具有状态500的错误而不是404错误。
我是否感到困惑,我没有使用正确的助手来生成图像路径?我已经在Ruby Gem中进行了调查,我没有看到任何改变行为的选项,因为在数据库中查找blob的ActiveRecord时出现了错误。以下是跟踪:

vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/core.rb:284:in `find': Couldn't find ActiveStorage::Blob with 'id'=1684686 (ActiveRecord::RecordNotFound)
    from vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/signed_id.rb:64:in `find_signed!'
    from vendor/bundle/ruby/3.1.0/gems/activestorage-7.0.4/app/models/active_storage/blob.rb:87:in `find_signed!'
    from vendor/bundle/ruby/3.1.0/gems/activestorage-7.0.4/app/controllers/concerns/active_storage/set_blob.rb:12:in `set_blob'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:400:in `block in make_lambda'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:199:in `block (2 levels) in halting'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:200:in `block in halting'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `block in invoke_before'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `each'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `invoke_before'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:116:in `block in run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.4/lib/action_text/rendering.rb:20:in `with_renderer'
    from vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.4/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:138:in `run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:233:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/rescue.rb:22:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `block in instrument'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `instrument'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:66:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:151:in `process'
    from vendor/bundle/ruby/3.1.0/gems/actionview-7.0.4/lib/action_view/rendering.rb:39:in `process'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal.rb:188:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal.rb:251:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:32:in `serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:50:in `block in serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `each'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:852:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/rack/browser_monitoring.rb:38:in `traced_call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/http_accept_language-2.1.1/lib/http_accept_language/middleware.rb:14:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/tempfile_reaper.rb:15:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/etag.rb:27:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/conditional_get.rb:27:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/http/permissions_policy.rb:38:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/http/content_security_policy.rb:36:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:266:in `context'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:260:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/cookies.rb:696:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:99:in `run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/bugsnag-6.25.0/lib/bugsnag/integrations/rack.rb:51:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:40:in `call_app'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `block in call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `block in tagged'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:37:in `tagged'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `tagged'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/request_store-1.5.1/lib/request_store/middleware.rb:19:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/request_id.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/method_override.rb:24:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/runtime.rb:22:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/sendfile.rb:110:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/ssl.rb:77:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/host_authorization.rb:131:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-utf8_sanitizer-1.8.0/lib/rack/utf8_sanitizer.rb:28:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/engine.rb:530:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/configuration.rb:268:in `call'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:89:in `block in handle_request'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:343:in `with_force_shutdown'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:88:in `handle_request'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:430:in `process_client'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:233:in `block in run'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:150:in `block in spawn_thread'

你有什么建议来更好地利用动态存储吗?你有没有在你的应用程序中以任何方式避免错误?
先谢谢你了!

zy1mlcev

zy1mlcev1#

检查这是否解决了您的问题:

Rails.application.routes.url_helpers.polymorphic_url(user.avatar)
hgtggwj0

hgtggwj02#

我刚刚意识到我把一个“500状态错误”和一个INFO级别的异常搞混了。应用程序的响应很好:它显示了一个404错误,但我的错误处理程序应用程序获得了一个INFO级别的异常,我将其解释为我的用户正在显示一个错误屏幕,而实际上应用程序处理情况很好。

相关问题