Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 955
Simon Mo Avatar asked Oct 08 '19 08:10

Simon Mo


2 Answers

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.

like image 151
user10704464 Avatar answered Nov 06 '22 18:11

user10704464


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

like image 43
Simon Mo Avatar answered Nov 06 '22 17:11

Simon Mo