问题描述
我的格式如下:
<%= 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
页面上正在调用它:
It is being called on my edit
page:
<h1>Upload Avatar</h1>
<%= image_tag(@user.avatar) %>
<%= render 'form', user: @user %>
<hr>
我收到标题错误,但是我不确定为什么头像没有附加到user
模型上.我已经完成了active_storage
的所有要求.
I get the error in the title but I am not sure why the avatar is not being attachd to the user
model. I have all the requirements done for active_storage
.
has_one_attached :avatar
.
在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
我也有所有必要的迁移.我是否缺少实际的头像附加逻辑?
Also I have all the necessary migrations. Am I missing the actual avatar attaching logic?
推荐答案
您会收到错误消息无法将图像解析为URL:to_model委托给附件,但附件为零" 您试图在视图中显示不存在的附件:
You can get the error message "Can't resolve image into URL: to_model delegated to attachment, but attachment is nil" if you are trying to show in your view an attachment that does not exist:
<%= image_tag(@user.avatar) %>
为避免错误,您应该执行以下操作:
to avoid error you should do this:
<%= image_tag(@user.avatar) if @user.avatar.attached? %>
这篇关于Rails:无法将图像解析为URL:to_model委托给附件,但是附件为nil Rails 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!