I'm having an issue with image/pdf uploads with ActiveStorage. The images appear to be uploading without issue, but they are causing errors when I try to show them.
My blog
model has_one_attached
:image
and has_one_attached
:pdf
. The uploads used to work (so I know I have ActiveStorage installed and my amazon s3 set up properly), but something has gone wrong.
The only complicated bit is I need it to work if it has a PDF or not (not all blogs will have a pdf...all should have an image).
My blog#create
method is:
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
My blog#update
method is:
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
My form is simple:
<%= 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 %>
I'm trying to show the image in the blog like this:
<div class="frame" style="background-image: url(<%= rails_blob_url(@blog.image) %>)"></div>
And the PDF like this:
<h2 class="cta text-center"><%= link_to @blog.cta, rails_blob_url(@blog.pdf), target: "_blank" %></h2>
The error I'm getting is signed_id delegated to attachment, but attachment is nil
on the place the image is called as a background image on the blog#show
page. I get the same error on localhost
and Heroku, if it helps.
Finally, I saw this error on this question and did try dropping and recreating my database, but to no avail.
Can anyone see what's going wrong here?
ActiveRecord::Base.include_root_in_json
set to true or false? – Menzies