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

2023-12-19

我有以下表格:

<%= 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 page:

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

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

has_one_attached :avatar in user model.

In 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 

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


你可以得到错误信息“无法将图像解析为 URL:to_model 委托给附件,但附件为零”如果您尝试在视图中显示不存在的附件:

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

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

<%= image_tag(@user.avatar) if @user.avatar.attached? %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将图像解析为 URL:to_model 委托给附件,但在 Rails 5.2 中附件为零 的相关文章

随机推荐