Simon Mo
Simon Mo

Reputation: 583

Rails 6 Active Storage : Could not find or build blob: expected attachable, got nil

Just created a new Rails 6 app, and I am trying to allow adding images to an active_storage blob instead of replacing them, through a form generated with rails scaffold.

Followed the documentation (https://guides.rubyonrails.org/active_storage_overview.html#has-many-attached), using #attach into my controller, but it leads to an error page and keep the "default" behavior of replacing all the images instead of adding new images.

Using Rails 6.0.0 with active_storage 6.0.0

I first made a Page model using rails g scaffold Page name:string and added then in my page.rb model the association with ActiveStorage has_many_attached :images

In my form I added a file_field, allowing multiple uploads:

<%= form.file_field :images, multiple: true %>

Here is my controller update action, note @page.images.attach(params[:images]) that is supposed to do the job, according to the documentation


def update

    respond_to do |format|
      if @page.update(page_params)

        @page.images.attach(params[:images])

        format.html { redirect_to site_pages_path(@site), notice: 'Page was successfully updated.' }
        format.json { render :show, status: :ok, location: @page }
      else
        format.html { render :edit }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

When filling the form, attaching new pictures and posting it, I got the following error :

ArgumentError in PagesController#update
Could not find or build blob: expected attachable, got nil 

Pointing the line @page.images.attach(params[:images])

When checking the server logs, I noticed that despite the error, the default behavior is still running : The old images get deleted and the new ones get attached.

Upvotes: 7

Views: 13152

Answers (3)

Rafael Gomes Francisco
Rafael Gomes Francisco

Reputation: 2322

I will let another way to solve that "more clear"

I create a private method on controller class:

before_action :append_images, only: %i[create update]

I assign that method to run on before_action macro:

def append_images
  return if params[:model_name][:images].blank?

  params[:model_name][:images].each do |image|
    @model_name.images.attach(image)
  end
end

And finally remove images: [] from permit params

Upvotes: 0

user10704464
user10704464

Reputation: 206

Under Rails 6, the default behavior for has_many_attached was changed from Rails 5. Previously, files were appended to the attachment list instead of being overridden.

Luckily this default can be changed, in your application.rb:

config.active_storage.replace_on_assign_to_many = false

You can then keep the images: [] section in the permitted list and remove the @page.images.attach(params[:images]) call completely.

Upvotes: 18

Simon Mo
Simon Mo

Reputation: 583

Ok, I could fix the issue !

The problem : update action will replace all the pictures.

So here is what I did :

1) I removed images: [] from the permitted list (strong parameters)

2) I wrote this code to attach individually each of the new images and put it into create and update actions :

    if params[:page][:images].present?
      params[:page][:images].each do |image|
        @page.images.attach(image)
      end
    end

#attach will actually permit the param.

Not sure it was the best way, but now it's working

Upvotes: 4

Related Questions