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.