Search code examples
ruby-on-railsrubyrails-activestorage

Rails 6 Active Storage Attachment 404 not found


I'm encountering a very strange issue in my Rails 6 application with Active Storage.

You can add a PDF file to a feedback, which is stored via Active Storage - in development this works without problems, you can save and open it.

It kind of works in production aswell, but the problem appears when I re-deploy the application with Capistrano. Before the deploy, all files saved up to this point are shown without problems. After the re-deploy, the files stored BEFORE are not found anymore - without any changes on the database.

That's my view code (I tried with url_for, rails_blob_path and service_url via controller aswell)

That's what my log says before the re-deploy (successfully shows the file):

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)

Then, after redeploy I get this 404 not found error on the exact same file:

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)

Any idea what is happening here? Maybe a problem with NGINX file caching or something?


Solution

  • Okay after digging into some research I found out why this happened - I'll try to explain it in case someone may be getting into similar issues when deploying with Capistrano.

    So, it's not directly a Rails or Active Storage related issue. I'm using local storage for my attachments, so I do not store them on Amazon or Google or Azure - the default storage location for those attachments is the 'storage' folder in the root of the application - so I checked storage.yml:

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

    And in my production.rb:

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

    My problem was the following: This folder itself is not added to .gitignore by default, just the contents inside the folder are ignored by GIT. So it happened that I pushed an empty storage folder to my repo every time I do a git push. This will of course override the storage folder which was used before in production on every deploy with Capistrano.

    What solved the problem in my case was to add the storage folder to the linked_dirs array, which you can specify inside the deploy.rb:

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

    With this option set, the folder will not be overwritten on every deploy, instead it will be shared between the releases and the previously uploaded files will stay accessible.

    I hope this will help somebody in the future, if you ask yourself why your files are disappearing :').