12

I have a Rails 6 app that uses Active Storage to store multiple images to a model (Activity) with has_many_attached.

I don't understand how to append extra images instead of replacing the existing images. When I upload images the first time they save correctly. However, when I update the record, and I add a new image, the previous images are replaced by the new image. In Rails 5 the new image would be appended without replacing the previously saved images.

How do I append a new image instead of replacing the previous images?

I have an Activity model that has the following:

has_many_attached :images

In the form I have:

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

In the controller I have the following:

def update
  @activity = Activity.find(params[:id])

  if @activity.update(activity_params)
    flash[:success] = "Saved"
    redirect_to activity_path(@activity)
  else
    flash[:error] = "Not saved"
    redirect_to edit_activity_path(@activity)
  end
end

private

  def activity_params
    params.require(:activity).permit(:name, images:[])
  end
don_Bigote
  • 896
  • 7
  • 33

3 Answers3

19

This is the solution that I have now:

Add this to the update action:

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

So, the entire update action looks like this:

def update
  if activity.update(activity_params)
    if params[:activity][:images].present?
      params[:activity][:images].each do |image|
        activity.images.attach(image)
      end
    end
    flash[:success] = 'Updated!'
    respond_with activity, location: activity_path(activity)
  else
    flash[:error] = 'Not updated'
    respond_with activity, location: activity_path(activity)
  end
end

And remove this from permitted params:

images:[]
don_Bigote
  • 896
  • 7
  • 33
  • How could the create action be set up? – don_Bigote Feb 01 '20 at 06:11
  • just to complement a little with some information, if you change `params[:activity][:images]` to `activity_params[:images] ` it will not work – gcs_dev Oct 03 '20 at 13:27
  • 1
    A bit late to the party but assuming you're on Rails 6 this seems to be the culprit: https://stackoverflow.com/questions/61933070/how-to-edit-multiple-attached-images-using-activestorage-has-many-attached-in – stratis Nov 02 '20 at 12:02
2

This happens cause assigning to attachments declared with has_many_attached since Rails 6.0 replaces any existing attachments by default.

Details:

So, in Rails 6.0+ you need to add this line to your environment config (config/environments/development.rb, etc.):
config.active_storage.replace_on_assign_to_many = false
After app restart previously added attachments won't be replaced.

Max Berdnikau
  • 33
  • 1
  • 1
  • 6
-5

Try selecting multiple files like this https://i.stack.imgur.com/EqaU7.png If it works then you will need to select all previous images while uploading new one which does not sound good. You may like to use nesting forms.

Sumra Yasmin
  • 9
  • 1
  • 2
  • The question here is: How do I append a new image instead of replacing the previous images? In Rails 6 the way to update multiple images for a given record has changed since Rails 5. Based on your answer you may not be familiar with this issue. – don_Bigote Jan 20 '20 at 19:32