Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update multiple files without replace #1844

Closed
Tecktoart opened this issue Jan 20, 2016 · 11 comments
Closed

Update multiple files without replace #1844

Tecktoart opened this issue Jan 20, 2016 · 11 comments
Labels

Comments

@Tecktoart
Copy link

Sorry, I bad speak English :)
Hi I have used this article for multiple upload files and now I have some problems with update files without replace.

https://www.codementor.io/tips/8367241728/carrierwave-now-natively-supports-multiple-file-uploads-in-rails-gotchas

gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
class AttachmentUploader < CarrierWave::Uploader::Base
  def store_dir
    "#{mounted_as}/#{model.id}"
  end
end
class Opportunity < ActiveRecord::Base
   ##field in database for attachments:json

    mount_uploaders :attachments, AttachmentUploader
end
module Companies
  class OpportunitiesController < ApplicationController
    before_action :set_company
    before_action :set_opportunity, only: [:edit, :update]

    def new
      @opportunity = @company.opportunities.build
      authorize @opportunity
    end

    def create
      @opportunity = @company.opportunities.build(opportunity_params)
      authorize @opportunity

      respond_to do |format|
        if @opportunity.save
          format.html { redirect_to opportunities_url, notice: 'Opportunity was successfully created.' }
          format.json { render :show, status: :created, location: @opportunity }
        else
          format.html { render :new }
          format.json { render json: @opportunity.errors, status: :unprocessable_entity }
        end
      end
    end

    def edit
    end

    def update
      respond_to do |format|
        if @opportunity.update(opportunity_params)
          format.html { redirect_to @opportunity, notice: 'Opportunity was successfully updated.' }
          format.json { render :show, status: :ok, location: @opportunity }
        else
          format.html { render :edit }
          format.json { render json: @opportunity.errors, status: :unprocessable_entity }
        end
      end
    end

    private
    def set_opportunity
      @opportunity = Opportunity.find(params[:id])
      authorize @opportunity
    end

    def opportunity_params
      params.require(:opportunity).permit(attachments: [])
    end

    def set_company
      @company = current_user.company
    end
  end
end
  = simple_form_for(@opportunity, wrapper: :horizontal_form) do |f|
    = f.file_field :attachments, class: 'form-control', multiple: true
    = f.button :submit, class: 'btn-primary', id: 'opportunity-btn'

When I update attachments, they to replace in database.

Can someone suggest a good solution for update form without replace?

Thanks
Artsiom Harashchenia
Email: tecktoart@gmail.com

@thomasfedb
Copy link
Contributor

Hi @Tecktoart, you haven't provided enough information in order for us to be ale to work out what you're trying to do.

I'm closing this issue until you can update with sufficient details.

@Tecktoart
Copy link
Author

@thomasfedb, I have updated, please verify

@thomasfedb
Copy link
Contributor

What do you mean by "replace"? Please demonstrate.

@Tecktoart
Copy link
Author

I have created opportunity with 2 files

image

My files
http://storage7.static.itmages.com/i/16/0120/h_1453302616_2373939_a431032a66.png

When I have added one file on update form, my files have replaced in database on last file from update form.
http://storage1.static.itmages.com/i/16/0120/h_1453302884_3381262_efff612664.png

My last file from update form
http://storage3.static.itmages.com/i/16/0120/h_1453303034_3683378_7b082e3a4b.png

@pawelduda
Copy link

+1 on this one, it appears that on multiple files upload, previously stored files are being replaced by newer one(s). Trying to find a way to hack around this but without any success so far.

@basmoura
Copy link

+1 I'm having the same issue here. I've tried to use this how to but the files still being replaced.

@elia
Copy link

elia commented Mar 10, 2017

Seems to be fixed by the following 🐒patch:

CarrierWave::Mounter.class_eval do
  def cache(new_files)
    return if not new_files or new_files == ""
    @uploaders = new_files.map do |new_file|
      if new_file.is_a?(uploader_class)
        uploader = new_file
        uploader
      else
        uploader = blank_uploader
        uploader.cache!(new_file)
        uploader
      end
    end

    @integrity_error = nil
    @processing_error = nil
  rescue CarrierWave::IntegrityError => e
    @integrity_error = e
    raise e unless option(:ignore_integrity_errors)
  rescue CarrierWave::ProcessingError => e
    @processing_error = e
    raise e unless option(:ignore_processing_errors)
  end
end
  
CarrierWave::Uploader::Base.class_eval do
  def identifier
    File.basename(path) if path
  end
end

@thomasfedb if it looks ok I can send a PR with the patch

@60mm
Copy link

60mm commented Aug 31, 2018

I solved this issue by adding this to the model with the images attached. I am a novice coder so I can't promise it is the fastest, but it does work. I have the images stored as JSON.

before_validation { self.previous_images }
before_save { self.add_previous_images }

def previous_images
  if self.images
    @images = self[:images]
  end
end  

def add_previous_images
  if defined?(@images)
    @images.each do |a|
      if !self[:images].include?(a)
        self[:images] << a
      end	
    end	
  end	
end	  

@syafilm
Copy link

syafilm commented May 6, 2019

+1 I'm having the same problem here. i was sending param with two params the first one is remote_url and the second one is params for file but always get replaced with the file

@mshibuya
Copy link
Member

Closed by #2401.

@TaylorHorn
Copy link

Closed by #2401.

I see you added a way to do this inside the view, but is there any way to achieve this in the model or controller for use cases with rails? We are not using erbs on our application so that solution in the README will not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants