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

只愿长相守 提交于 2020-01-24 15:49:05

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/58282845/rails-6-active-storage-could-not-find-or-build-blob-expected-attachable-got

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!