Liz*_*Liz 10 ruby-on-rails-5 rails-activestorage
我在使用ActiveStorage上传图像/ pdf时出现问题.图像似乎没有问题上传,但是当我尝试显示它们时它们会导致错误.
我的blog模特has_one_attached :image和has_one_attached :pdf.上传曾经工作(所以我知道我安装了ActiveStorage,我的亚马逊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
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
我的表格很简单:
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
我试图在博客中显示这样的图像:
<div class="frame" style="background-image: url(<%= rails_blob_url(@blog.image) %>)"></div>
Run Code Online (Sandbox Code Playgroud)
和PDF这样:
<h2 class="cta text-center"><%= link_to @blog.cta, rails_blob_url(@blog.pdf), target: "_blank" %></h2>
Run Code Online (Sandbox Code Playgroud)
我得到的错误是signed_id delegated to attachment, but attachment is nil在图像被称为blog#show页面背景图像的地方.localhost如果有帮助,我和Heroku 会出现同样的错误.
最后,我在这个问题上看到了这个错误,并尝试删除并重新创建我的数据库,但无济于事.
谁能看到这里出了什么问题?
我刚刚遇到了这个错误,并且真的很难弄清楚可能发生了什么。它第一次出现是在我提交表单并且不包含附件时。原来我需要检查一下是否真的附加了某些东西并处理这种可能性。
也许尝试将@blog.pdf.attach(params[:pdf]) 移到 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 %>
Run Code Online (Sandbox Code Playgroud)
Heroku 在此处有一篇关于主动存储的好文章,也可能有所帮助。
| 归档时间: |
|
| 查看次数: |
1276 次 |
| 最近记录: |