need help with carrierwave - multiple vs single file upload

11 views
Skip to first unread message

tom

unread,
Nov 1, 2019, 11:44:17 AM11/1/19
to Ruby on Rails: Talk
hi,

i want to have one uploader, but 2 forms:

<h3>Upload Single Attachment</h3>
<%= simple_form_for(FolderAttachment.new) do |form| %>
  <%= form.error_notification %>

  <div class="form-inputs">
  <%= form.file_field :singlefile %>
  </div>

  <%= form.input :folder_id , :as => :hidden , :input_html => { :value => @folder_current.id}  if @folder_current %>

  <div class="form-inputs">
  <%= form.input :comment %>
  </div>

  <div class="form-inputs">
  <%= form.input :tags %>
  </div>


  <div class="form-actions">
    <%= form.button :submit %>
  </div>
<% end %>


<h3>Upload Multiple Attachments</h3>
<%= simple_form_for(FolderAttachment.new, :html => { :multipart => true } ) do |form| %>
  <%= form.error_notification %>

  <div class="form-inputs">
  <%= form.file_field :files, multiple: true, name: "folder_attachments[files][]"  %>
  </div>

  <%= form.input :folder_id , :as => :hidden , :input_html => { :value => @folder_current.id}  if @folder_current %>

  <div class="form-actions">
    <%= form.button :submit %>
  </div>
<% end %>



--> no matter what i do, i always get errors on controller leve, eg: map or others. can an uploader only have 1 mount?

class FolderAttachment < ApplicationRecord

belongs_to :folder


  mount_uploaders  :singlefile, FolderAttachmentUploader

mount_uploaders  :files, FolderAttachmentUploader



end

thx

Ariel Juodziukynas

unread,
Nov 1, 2019, 12:23:32 PM11/1/19
to rubyonra...@googlegroups.com
"always get errors", show the errors then

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CADQqhMenWxfarO9dCY%2B1N98tmMQuXtysykGj58tV%2B83pqNUEOA%40mail.gmail.com.

Ariel Juodziukynas

unread,
Nov 1, 2019, 12:24:35 PM11/1/19
to rubyonra...@googlegroups.com
Also, use "mount_uploader" singular for the single file and "mount_uploaders" plural for the multiple files

El vie., 1 nov. 2019 a las 12:44, tom (<toma...@gmail.com>) escribió:

tom

unread,
Nov 1, 2019, 1:19:14 PM11/1/19
to Ruby on Rails: Talk
ok, the plural got me beyond an error message, but i cant see the uploaded multiuploaded files. singles files work.

controller code:
    @folder_attachment = FolderAttachment.new(folder_attachment_params)
    @folder_attachment.company_id = current_user.cid
    @folder_attachment.user_id = current_user.id
     
     
    @folder = Folder.find_by_id(params[:folder_attachment][:folder_id])
     
    respond_to do |format|
      if @folder_attachment.save
        format.html { redirect_to @folder , notice: 'Attachment was successfully created.' }
      #  format.json { render :show, status: :created, location: @folder_attachment }
      else
        format.html { render :new }
       # format.json { render json: @folder_attachment.errors, status: :unprocessable_entity }
      end
    end


    def folder_attachment_params
      params.require(:folder_attachment).permit(:company_id, :folder_id, :singlefile, :comment,:tags, {files: []})
    end
==========
class FolderAttachmentUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
 
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end



any ideas?

thx



Ariel Juodziukynas

unread,
Nov 1, 2019, 1:30:38 PM11/1/19
to rubyonra...@googlegroups.com
I think your problem is here

<%= form.file_field :files, multiple: true, name: "folder_attachments[files][]"  %>

use "folder_attachment[files][]", note "folder_attachment" singular, not plural since the form is for a single folder_attachment (and that's on your folder_attachment_params method)

tom

unread,
Nov 1, 2019, 1:41:49 PM11/1/19
to Ruby on Rails: Talk
yep - its working.
but, can i get the array of filenames in case of multiupload in to the same db field and or vice versa? eg:

mount_uploader  :files, FolderAttachmentUploader
mount_uploaders  :files, FolderAttachmentUploader


Ariel Juodziukynas

unread,
Nov 1, 2019, 1:54:02 PM11/1/19
to rubyonra...@googlegroups.com
I don't think you can use the same attribute name for single and multiple attachments at the same time. What you can do is to always have multiple files and attach only one file when you get a single file upload. I don't understand the use case of your idea.

Ariel Juodziukynas

unread,
Nov 1, 2019, 1:55:23 PM11/1/19
to rubyonra...@googlegroups.com
I mean: multiple files can be zero or more, it includes the possibility of having only 1 file attached.

tom

unread,
Nov 1, 2019, 1:55:44 PM11/1/19
to Ruby on Rails: Talk
usecase:
if i do multiupload, how could i add comments and additional fields to 'each' file during upload!?



Ariel Juodziukynas

unread,
Nov 1, 2019, 1:59:40 PM11/1/19
to rubyonra...@googlegroups.com
If you need extra fields for each attachment then you need an extra model to store that data and the carrierwave file for each file you upload. Given the name of the model "FolderAttachment" it looks like that's the model and that it shouldn't have multiple files, it looks like there should be multiple FolderAttachments with only one file each.

Reply all
Reply to author
Forward
0 new messages