变体委托给附件,但附件为零

variant delegated to attachment, but attachment is nil

我正在使用 ActiveStorage 开发 Rails 6 应用程序。我正在使用 devise 进行身份验证,这需要 sign_up 页面上的电子邮件和密码。成功注册后,我想重定向到编辑个人资料。用户不一定需要上传头像或其他一些字段。如果这是用户的选择,这些可以留空。

ActionView::Template::Error (variant delegated to attachment, but attachment is nil):
    3: 
    4: <p><%= @user.full_name %><p>
    5: <p>
    6:   <%= image_tag @user.avatar.variant(resize_to_limit: [100, 100])%>
    7: <p>
    8: <p><%= @user.city %><p>
    9: <p><%= @user.bio %><p>

我收到以下错误,但我希望这些字段是可选的,我该如何实现? users/edit.html.erb

  <%= form_for @user do |f| %>
    <div class="form-group">
      <%= f.label :avatar %>
      <%= f.file_field :avatar, as: :file, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :full_name, 'Full Name' %>
      <%= f.text_field :full_name, autofocus: true, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :city, 'City' %>
      <%= f.text_field :city, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :bio, 'Bio'%>
        <p> Why did you join ArtsySpace?
        What should other people here know about you?
        </p>
      <%= f.text_field :bio, class: "form-control"%>
    </div>

    <div class="form-group">
      <%= f.submit "Edit profile", class: "btn btn-primary" %>
    </div>
  <% end %>

我有以下表格。如果没有上传头像,我希望不会在 show.html.erb 页面中抛出 nil 错误。

你用 attached?

检查这个
<% if @user.avatar.attached? %>
  <%= image_tag @user.avatar.variant(resize_to_limit: [100, 100])%>
<% end %>