获取Blob的网址时image / png的未知格式错误

时间:2019-08-01 07:21:37

标签: ruby-on-rails rails-activestorage ruby-on-rails-5.2

我收到此错误:

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

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

当我尝试访问Blob的网址时。 我已将图像文件(.png)上传到活动存储中,并且可以在数据库和本地文件系统中看到它。 “ product.image.attached?”返回true。但是,当我尝试访问它(显示在页面上)时,出现上述错误。

我尝试过:

= 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

我得到了网址: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

这会导致错误。

如果我想念什么,请告诉我。谢谢

编辑: 我在日志中得到了这个

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)

再往下走:

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"}

1 个答案:

答案 0 :(得分:0)

感谢this answer,我已经完成了工作。我只需要将routes.rb底部的一行更改为此:

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

或者在我的情况下是

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