ruby-on-rails 未找到Rails 6 Active Storage附件404

2w3rbyxf  于 2023-06-25  发布在  Ruby
关注(0)|答案(1)|浏览(190)

我在使用Active Storage的Rails 6应用程序中遇到了一个非常奇怪的问题。
您可以将PDF文件添加到反馈中,该文件通过Active Storage存储-在开发过程中,这不会有任何问题,您可以保存并打开它。
它在生产环境中也可以工作,但是当我用Capistrano重新部署应用程序时,问题就出现了。在部署之前,到目前为止保存的所有文件都显示为没有问题。在重新部署之后,将找不到之前存储的文件-数据库没有任何更改。
这是我的视图代码(我也通过控制器尝试了url_forrails_blob_pathservice_url
这就是我的日志在重新部署之前所说的(成功地显示了文件):

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhNREZuWjJNek9HeDZNV1ZvT0RWaFkyZDFkVGQ2YW04NU5XdzBkQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWkdsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWxkbFlrMWxiblZsSUZCeWIzTmxiblJoZEdsdmJpNXdaR1lpT3lCbWFXeGxibUZ0WlNvOVZWUkdMVGduSjFkbFlrMWxiblZsSlRJd1VISWxRek1sUWpWelpXNTBZWFJwYjI0dWNHUm1CanNHVkRvUlkyOXVkR1Z1ZEY5MGVYQmxTU0lVWVhCd2JHbGpZWFJwYjI0dmNHUm1CanNHVkE9PSIsImV4cCI6IjIwMjAtMDctMTRUMTg6MDg6MjIuOTQ5WiIsInB1ciI6ImJsb2Jfa2V5In19--0a52cfc77ce14105dc290ff569d50b637231f613/WebMenue%20Pr%C3%B5sentation.pdf?content_type=application%2Fpdf&disposition=inline%3B+filename%3D%22WebMenue+Prosentation.pdf%22%3B+filename%2A%3DUTF-8%27%27WebMenue%2520Pr%25C3%25B5sentation.pdf" for 172.16.118.100 at 2020-07-14 20:03:48 +0200

Processing by ActiveStorage::DiskController#show as PDF

Parameters: {"content_type"=>"application/pdf", "disposition"=>"inline; filename=\"WebMenue Prosentation.pdf\"; filename*=UTF-8''WebMenue%20Pr%C3%B5sentation.pdf", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhNREZuWjJNek9HeDZNV1ZvT0RWaFkyZDFkVGQ2YW04NU5XdzBkQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWkdsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWxkbFlrMWxiblZsSUZCeWIzTmxiblJoZEdsdmJpNXdaR1lpT3lCbWFXeGxibUZ0WlNvOVZWUkdMVGduSjFkbFlrMWxiblZsSlRJd1VISWxRek1sUWpWelpXNTBZWFJwYjI0dWNHUm1CanNHVkRvUlkyOXVkR1Z1ZEY5MGVYQmxTU0lVWVhCd2JHbGpZWFJwYjI0dmNHUm1CanNHVkE9PSIsImV4cCI6IjIwMjAtMDctMTRUMTg6MDg6MjIuOTQ5WiIsInB1ciI6ImJsb2Jfa2V5In19--0a52cfc77ce14105dc290ff569d50b637231f613", "filename"=>"WebMenue Prõsentation"}

Completed 304 Not Modified in 1ms (ActiveRecord: 0.0ms | Allocations: 370)

然后,在重新部署后,我在完全相同的文件上得到这个404未找到错误:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhNREZuWjJNek9HeDZNV1ZvT0RWaFkyZDFkVGQ2YW04NU5XdzBkQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWkdsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWxkbFlrMWxiblZsSUZCeWIzTmxiblJoZEdsdmJpNXdaR1lpT3lCbWFXeGxibUZ0WlNvOVZWUkdMVGduSjFkbFlrMWxiblZsSlRJd1VISWxRek1sUWpWelpXNTBZWFJwYjI0dWNHUm1CanNHVkRvUlkyOXVkR1Z1ZEY5MGVYQmxTU0lVWVhCd2JHbGpZWFJwYjI0dmNHUm1CanNHVkE9PSIsImV4cCI6IjIwMjAtMDctMTRUMTg6MTI6MzQuNTE3WiIsInB1ciI6ImJsb2Jfa2V5In19--9d281265e57a7c0d3cd53af1f8e1a6e098605d3c/WebMenue%20Pr%C3%B5sentation.pdf?content_type=application%2Fpdf&disposition=inline%3B+filename%3D%22WebMenue+Prosentation.pdf%22%3B+filename%2A%3DUTF-8%27%27WebMenue%2520Pr%25C3%25B5sentation.pdf" for 172.16.118.100 at 2020-07-14 20:07:34 +0200

Processing by ActiveStorage::DiskController#show as PDF

Parameters: {"content_type"=>"application/pdf", "disposition"=>"inline; filename=\"WebMenue Prosentation.pdf\"; filename*=UTF-8''WebMenue%20Pr%C3%B5sentation.pdf", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhNREZuWjJNek9HeDZNV1ZvT0RWaFkyZDFkVGQ2YW04NU5XdzBkQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWkdsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWxkbFlrMWxiblZsSUZCeWIzTmxiblJoZEdsdmJpNXdaR1lpT3lCbWFXeGxibUZ0WlNvOVZWUkdMVGduSjFkbFlrMWxiblZsSlRJd1VISWxRek1sUWpWelpXNTBZWFJwYjI0dWNHUm1CanNHVkRvUlkyOXVkR1Z1ZEY5MGVYQmxTU0lVWVhCd2JHbGpZWFJwYjI0dmNHUm1CanNHVkE9PSIsImV4cCI6IjIwMjAtMDctMTRUMTg6MTI6MzQuNTE3WiIsInB1ciI6ImJsb2Jfa2V5In19--9d281265e57a7c0d3cd53af1f8e1a6e098605d3c", "filename"=>"WebMenue Prõsentation"}

Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms | Allocations: 799)

知道这里发生了什么吗也许是NGINX文件缓存的问题?

izkcnapc

izkcnapc1#

好吧,在深入研究之后,我发现了为什么会发生这种情况-我将尝试解释它,以防有人在部署Capistrano时遇到类似的问题。
所以,这不是Rails或Active Storage直接相关的问题。我使用本地存储来存储附件,所以我不会将它们存储在Amazon、Google或Azure上--这些附件的默认存储位置是应用程序根目录中的“storage”文件夹--所以我选中了storage.yml

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

production.rb中:

# Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

我的问题如下:该文件夹本身默认添加到.gitignore中,只是文件夹内的内容被GIT忽略。因此,每当我执行git push时,我都会将一个空的存储文件夹推送到我的存储库中。这当然会覆盖**以前在生产中使用Capistrano的每次部署时使用的存储文件夹。
在我的例子中,解决问题的方法是将存储文件夹添加到linked_dirs阵列中,您可以在deploy.rb中指定该文件夹:

append :linked_dirs, 'log', 'storage', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads', 'node_modules'

设置此选项后,该文件夹不会在每次部署时被覆盖,而是在版本之间共享,并且以前上传的文件将保持可访问性。
我希望这将帮助某人在未来,如果你问自己为什么你的文件消失:')。

相关问题