来自 ActiveStorage 的文件未显示 "Missing Template" 错误

Files from ActiveStorage not displaying with "Missing Template" error

Rails v5.2.1;使用 DirectUpload 将文件上传到私有 AWS 存储桶,但我认为这不是我的问题。

我有一个包含 has_one_attached :avatar 的用户模型。图片上传没有问题;我在我的存储桶和我的数据库的 ActiveStorage 表中看到它。

稍后我尝试像这样显示上传的头像:

<%= image_tag url_for(current_user.avatar) %>
(I've also tried this):
<img src="<%= url_for(current_user.avatar) -%>" />

这会生成一个 URL,类似于 http://localhost:3000/rails/active_storage/blobs/[a hash, I assume]/[my file name].png

但是...没有显示图像。如果我尝试在新选项卡中打开图片,我会看到 "Template Is Missing" 错误页面。

Missing template /application with {:locale=>[:en], :formats=>[:png], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "/Users/matt/projects/project/app/views" * "/Users/matt/.rvm/gems/ruby-2.5.1/gems/kaminari-core-1.1.1/app/views" * "/Users/matt/.rvm/gems/ruby-2.5.1/gems/devise-4.4.3/app/views"

Extracted source:

def index
  render template: 'application'
end

现在...我确实有一个 application.html 模板,但这似乎是在寻找一个 application.png 模板,这似乎不对。我错过了什么?

查看

<%= image_tag url_for(current_user.avatar_url) %>

用户模型

#user.rb
class User < ApplicationRecord
    #...
    def avatar_url
      if self.avatar.attached?
        Rails.application.routes.url_helpers.rails_blob_path(self.avatar, only_path: true)
        #or
        self.avatar.service_url
      else
        nil
      end
    end
end