Search code examples
ruby-on-railsrails-activestorageruby-on-rails-5.2

Unknown format error for image/png when getting url for blob


I am getting this error:

ActionController::UnknownFormat (ErrorPagesController#not_found is missing a template for this request format and variant.

request.formats: ["image/png"]
request.variant: []):

when I try to access the url of a blob. I have uploaded an image file (.png) to active storage, and I can see it in my database and on my local filesystem. 'product.image.attached?' returns true. However, when I try to access it (to display on my page), I get the above error.

I have tried:

= image_tag url_for(product.pic)
= image_tag url_for(Rails.application.routes.url_helpers.rails_blob_path(product.image, only_path: true))
= image_tag url_for(product.image.service_url)
= link_to 'download', rails_blob_path(product.image, disposition: "attachment")

def pic_url
  if self.pic.attached?
    # Rails.application.routes.url_helpers.rails_blob_url(self.image, only_path: true)
    ActiveStorage::Current.set(host: "localhost:3000") do
      self.pic.service_url
    end
  end
end

I get the url: http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWRiWGQ2VUhGWWVEZExaVFZ3VTFGeWVrZFRZVUpFUWtaeUJqb0dSVlE2RUdScGMzQnZjMmwwYVc5dVNTSmJhVzVzYVc1bE95Qm1hV3hsYm1GdFpUMGljM05qWDJ4dloyOWZkMmhwZEdWZmMyMWhiR3d1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHpjMk5mYkc5bmIxOTNhR2wwWlY5emJXRnNiQzV3Ym1jR093WkdPaEZqYjI1MFpXNTBYM1I1Y0dWSklnNXBiV0ZuWlM5d2JtY0dPd1pVIiwiZXhwIjoiMjAxOS0wOC0wMVQwNzoxMjo1NC41NDRaIiwicHVyIjoiYmxvYl9rZXkifX0=--59b28fbf1b78f8d0e408040deb9f538473b6fd6f/ssc_logo_white_small.png?content_type=image%2Fpng&disposition=inline%3B+filename%3D%22ssc_logo_white_small.png%22%3B+filename%2A%3DUTF-8%27%27ssc_logo_white_small.png

which leads to the error.

If there is anything I'm missing, please let me know. Thanks

Edit: I get this in my logs:

Disk Storage (372.6ms) Generated URL for file at key: mwzPqXx7Ke5pSQrzGSaBDBFr (http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWRiWGQ2VUhGWWVEZExaVFZ3VTFGeWVrZFRZVUpFUWtaeUJqb0dSVlE2RUdScGMzQnZjMmwwYVc5dVNTSmJhVzVzYVc1bE95Qm1hV3hsYm1GdFpUMGljM05qWDJ4dloyOWZkMmhwZEdWZmMyMWhiR3d1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHpjMk5mYkc5bmIxOTNhR2wwWlY5emJXRnNiQzV3Ym1jR093WkdPaEZqYjI1MFpXNTBYM1I1Y0dWSklnNXBiV0ZuWlM5d2JtY0dPd1pVIiwiZXhwIjoiMjAxOS0wOC0wMVQwNzozNjozMi44NjFaIiwicHVyIjoiYmxvYl9rZXkifX0=--566d40e954f9f1103d1d515999cee2ff5ed745de/ssc_logo_white_small.png?content_type=image%2Fpng&disposition=inline%3B+filename%3D%22ssc_logo_white_small.png%22%3B+filename%2A%3DUTF-8%27%27ssc_logo_white_small.png)

and further down:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWRiWGQ2VUhGWWVEZExaVFZ3VTFGeWVrZFRZVUpFUWtaeUJqb0dSVlE2RUdScGMzQnZjMmwwYVc5dVNTSmJhVzVzYVc1bE95Qm1hV3hsYm1GdFpUMGljM05qWDJ4dloyOWZkMmhwZEdWZmMyMWhiR3d1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHpjMk5mYkc5bmIxOTNhR2wwWlY5emJXRnNiQzV3Ym1jR093WkdPaEZqYjI1MFpXNTBYM1I1Y0dWSklnNXBiV0ZuWlM5d2JtY0dPd1pVIiwiZXhwIjoiMjAxOS0wOC0wMVQwNzozNjozMi44NjFaIiwicHVyIjoiYmxvYl9rZXkifX0=--566d40e954f9f1103d1d515999cee2ff5ed745de/ssc_logo_white_small.png?content_type=image%2Fpng&disposition=inline%3B+filename%3D%22ssc_logo_white_small.png%22%3B+filename%2A%3DUTF-8%27%27ssc_logo_white_small.png" for ::1 at 2019-08-01 17:31:36 +1000
Processing by ErrorPagesController#not_found as PNG
  Parameters: {"content_type"=>"image/png", "disposition"=>"inline; filename=\"ssc_logo_white_small.png\"; filename*=UTF-8''ssc_logo_white_small.png", "a"=>"rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWRiWGQ2VUhGWWVEZExaVFZ3VTFGeWVrZFRZVUpFUWtaeUJqb0dSVlE2RUdScGMzQnZjMmwwYVc5dVNTSmJhVzVzYVc1bE95Qm1hV3hsYm1GdFpUMGljM05qWDJ4dloyOWZkMmhwZEdWZmMyMWhiR3d1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHpjMk5mYkc5bmIxOTNhR2wwWlY5emJXRnNiQzV3Ym1jR093WkdPaEZqYjI1MFpXNTBYM1I1Y0dWSklnNXBiV0ZuWlM5d2JtY0dPd1pVIiwiZXhwIjoiMjAxOS0wOC0wMVQwNzozNjozMi44NjFaIiwicHVyIjoiYmxvYl9rZXkifX0=--566d40e954f9f1103d1d515999cee2ff5ed745de/ssc_logo_white_small"}

Solution

  • I've worked it out thanks to this answer. I just needed to change a line at the bottom of routes.rb to this:

    get '*path', to: redirect('/'), constraints: lambda { |req|
      req.path.exclude? 'rails/active_storage'
    }
    

    or in my case it was

    get '*a', to: redirect('/'), constraints: lambda { |req|
      req.path.exclude? 'rails/active_storage'
    }