Rails 5.2 的 ActiveStorage 映像上传错误:signed_id委派给附件,但附件为零



我在使用 ActiveStorage 上传图像/pdf 时遇到问题。 这些图像似乎正在上传而没有问题,但是当我尝试显示它们时,它们会导致错误。

我的blog模型has_one_attached:imagehas_one_attached:pdf。 上传曾经工作过(所以我知道我已经安装了 ActiveStorage 并且我的 amazon s3 设置正确(,但出了点问题。

唯一复杂的一点是,如果它有没有 PDF,我需要它工作(并非所有博客都有 pdf......都应该有一个图像(。

我的blog#create方法是:

def create
@blog = Blog.new(blog_params)
@blog.user_id = current_user.id
if @blog.published
@blog.published_on = DateTime.current
end
respond_to do |format|
if @blog.save
if @blog.image.attached?
@blog.image.purge
end
@blog.image.attach(params[:image])
if @blog.pdf.attached?
@blog.pdf.purge
end
@blog.pdf.attach(params[:pdf])
format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
format.json { render :show, status: :created, location: @blog }
else
format.html { render :new }
format.json { render json: @blog.errors, status: :unprocessable_entity }
end
end
end

blog#update方法是:

def update
if @blog.published
@blog.published_on = DateTime.current
end
if @blog.image.attached?
@blog.image.purge
end
@blog.image.attach(params[:image])
if @blog.pdf.attached?
@blog.pdf.purge
end
@blog.pdf.attach(params[:pdf])
respond_to do |format|
if @blog.update(blog_params)
format.html { redirect_to @blog, notice: 'Blog was successfully updated.' }
format.json { render :show, status: :ok, location: @blog }
else
format.html { render :edit }
format.json { render json: @blog.errors, status: :unprocessable_entity }
end
end
end

我的表格很简单:

<%= simple_form_for(@blog) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
...
<div class="form-group">
<%= f.label "Blog Image" %><br />
<%= f.file_field :image %>
</div>
<div class="form-group">
<%= f.label "Linked PDF" %><br />
<%= f.file_field :pdf %>
</div>
...
<div class="form-actions text-center">
<%= f.button :submit, class: "btn-outline-primary" %>
</div>
<% end %>

我试图在博客中显示图像,如下所示:

<div class="frame" style="background-image: url(<%= rails_blob_url(@blog.image) %>)"></div>

而 PDF 是这样的:

<h2 class="cta text-center"><%= link_to @blog.cta, rails_blob_url(@blog.pdf), target: "_blank" %></h2>

我得到的错误signed_id delegated to attachment, but attachment is nilblog#show页面上将图像称为背景图像的位置。 如果有帮助,我在localhost和 Heroku 上收到同样的错误。

最后,我在这个问题上看到了这个错误,并尝试删除并重新创建我的数据库,但无济于事。

谁能看到这里出了什么问题?

我刚刚遇到了这个错误,真的很难弄清楚会发生什么。它第一次出现是在我提交表格并且不包含附件时。事实证明,我需要检查一下是否真的附加了某些东西并处理这种可能性。

也许尝试将 @blog.pdf.attach(params[:p df]( 移动到 blog#create 中的respond_to之前

然后,当尝试显示图像时,也许您可以尝试这样的事情

<% if blog.pdf.attached? == false %>
<p>No pdf attached</p>
<% elsif blog.pdf.previewable? %>
<%= link_to(image_tag(blog.pdf.preview(resize: "50x50>")),  rails_blob_path(blog.pdf, disposition: "attachment"))
%>
<% elsif blog.pdf.variable? %>
<%= link_to(image_tag(blog.pdf.variant(resize: "50x50")), rails_blob_path(blog.pdf, disposition: "attachment"))%>
<% else %>
<%= link_to "Download file", rails_blob_path(@blog.pdf, disposition: "attachment") %>
<% end %>

Heroku 这里有一篇关于活动存储的好文章,也可能有所帮助。

最新更新