ActiveStorage Image Upload Error with Rails 5.2: signed_id delegated to attachment, but attachment is nil
Asked Answered
G

1

10

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?

Galatians answered 20/9, 2018 at 1:59 Comment(2)
How come you are purging the uploaded image and then reattaching it?Millen
Can you show us the complete controller code? Specifically the strong parameters?Also, do you have ActiveRecord::Base.include_root_in_json set to true or false?Menzies
D
5

I just ran into this error and really struggled to figure out what could have been happening. It first appeared when I submitted a form and did not include an attachment. Turns out I needed to check to see if something was really attached and deal with that possibility.

Perhaps try moving @blog.pdf.attach(params[:pdf]) to before the respond_to in blog#create

Then, when trying to show the image, maybe you could try something like this

<% 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 has a good article on active storage here that may also help.

Disforest answered 10/7, 2019 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.