无法将图像解析为 URL:to_model 已委托给附件,但附件在 Rails 5.2 中为零

Can't resolve image into URL: to_model delegated to attachment, but attachment is nil in Rails 5.2

我有以下表格:

<%= form_with(model: user, local: true) do |form| %>
  <% if user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.file_field :avatar %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

它正在我的 edit 页面上调用:

<h1>Upload Avatar</h1>
  <%= image_tag(@user.avatar) %>
  <%= render 'form', user: @user %>
<hr>

我在标题中收到错误,但我不确定为什么头像没有附加到 user 模型。我已完成 active_storage.

的所有要求

has_one_attached :avataruser model.

user controller中:

  def identity_params
    params.permit(:email_confirmation, :password, :password_confirmation, :avatar).to_h.symbolize_keys.tap do |params|
      params[:email] = params[:email_confirmation]
    end 
  end 

我也有所有必要的迁移。我是否缺少实际的头像附加逻辑?

您似乎缺少配置(因为您没有提及):

您必须在 config/storage.yml

中声明 Active Storage 服务

来自文档的示例:

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

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

amazon:
  service: S3
  access_key_id: ""
  secret_access_key: ""

并且您必须通过设置 Rails.application.config.active_storage.service

告诉 Active Storage 使用哪个服务

Because each environment will likely use a different service, it is recommended to do this on a per-environment basis. To use the disk service from the previous example in the development environment, you would add the following to config/environments/development.rb:

# Store files locally.
config.active_storage.service = :local

如果您试图在您的视图中显示不存在的附件,您会收到错误消息 "Can't resolve image into URL: to_model delegated to attachment, but attachment is nil"

<%= image_tag(@user.avatar) %>

为避免错误,您应该这样做:

<%= image_tag(@user.avatar) if @user.avatar.attached? %>

由于这个愚蠢的错误,我也遇到了同样的错误。实际上我将额外的 space 添加到 local: 键。因此,请确保您遵循正确的缩进。并且不要错过配置。您必须按以下方式在 config/storage.yml 中声明 Active Storage 服务。

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

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

amazon:
  service: S3
  access_key_id: ""
  secret_access_key: ""
  bucket: ""
  region: "" # e.g. 'us-east-1'

确保在 environment/development.rb、environment/production.rb 和 environment/test.rb 中添加了以下代码行

config.active_storage.service = :local #environment/development.rb
config.active_storage.service = :amazon ##environment/production.rb
config.active_storage.service = :test #environment/test.rb

解决方案如果您的用户模型 ID 是 UUID

您需要稍微修改 ActiveStorage 迁移,通过 record 字段

添加 type: :uuid 到 ActiveStorage 迁移
create_table :active_storage_attachments do |t|
  t.string     :name,     null: false
  t.references :record,   null: false, polymorphic: true, index: false, type: :uuid
  ...

引用https://edgeguides.rubyonrails.org/active_storage_overview.html#setup