Reputation: 115
I have implemented the "multiple file uploads" in my model just like explained in the documentation of CarrierWave and it's working just fine. My problem is that I can't get the model update working. When I try to add new files it removes the old ones. I'd like to keep both. Here is part of my model and controller:
class Album < ActiveRecord::Base
mount_uploaders :photos, PhotosUploader
end
class AlbumController < ApplicationController
def create
@album = Album.new(album_params)
if @album.save
flash[:success] = 'Album created'
redirect_to @album
else
render 'new'
end
end
def update
@album = Album.find(params[:id])
if @album.update_attributes(album_params)
flash[:success] = 'Album created'
redirect_to @album
else
render 'edit'
end
end
private
def album_params
params.require(:album).permit({ photos: [] })
end
end
I thought about putting the photos in a different model but if I could make it work this way would be better. Any suggestions?
Upvotes: 1
Views: 240
Reputation: 41
I have the below in my update method to ensure existing images (avatars) uploaded by CarrierWave remain in tact. I have a separate method that allows users to individually delete images.
def update
project_params_holder = project_params
project_params_holder[:avatars] += @project.avatars if project_params_holder[:avatars]
respond_to do |format|
if @project.update(project_params_holder)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
Upvotes: 2