Refile gem : multiple file uploads - ruby-on-rails

I'm using Refile with Rails 4. I'm following their tutorial for multiple image upload. Each Post can have multiple Images. My models look like this:
Post.rb:
has_many :images, dependent: :destroy
accepts_attachments_for :images, attachment: :file
Image.rb:
belongs_to :post
attachment :file
I can upload files, fine by using:
<%= f.attachment_field :images_files, multiple: true, direct: true, presigned: true %>
but when I try to retrieve an image like:
<%= attachment_image_tag(#post.images, :file, :small) %>
I get the error:
undefined method file for #<Image::ActiveRecord_Associations_CollectionProxy:0x007fbaf51e8ea0>
How can I retrieve an image with refile using multiple image upload?

In order to retrieve images who belongs to a post, you need to iterate through the array of images.
<% #post.images.each do |image| %>
<%= attachment_image_tag(image, :file, :fill, 300, 300) %>
<% end %>
The helper attachment_image_tag take:
[Refile::Attachment] object : Instance of a class which has an attached file.
[Symbol] name : The name of the attachment column
So here, #posts.images holds an array of image object. It's that object who has an attached file.
class Image < ActiveRecord::Base
belongs_to :post
attachment :file
end
Then when you iterate images, you give to the helper the image object, and the name of the attachment column, here :file.

Are you on the master branch?
gem 'refile', require: "refile/rails", git: 'https://github.com/refile/refile.git', branch: 'master'

Related

ActiveStorage multiple upload with intermediate model

The goal
I want to upload multiple files. So one Intervention can have multiple Uploads and each Upload has one attached file to it. This way, each Upload can have one file attached with different status, name, visibility, etc. instead of having one Upload with has_many_attached
What I have done
I have one Intervention model that can have many uploads :
class Intervention < ApplicationRecord
has_many :uploads, dependent: :destroy
accepts_nested_attributes_for :uploads, :allow_destroy => true
end
Each uploads has one attached file using ActiveStorage :
class Upload < ApplicationRecord
belongs_to :intervention
has_one_attached :file
end
In my interventions_controller I do :
def new
#intervention = Intervention.new
#intervention.uploads.build
end
def create
#intervention = Intervention.new(intervention_params)
# + default scaffolded controller [...]
end
def intervention_params
params.require(:intervention).permit(:user_id, :comment, uploads_attributes: [:status, :file])
end
In my form I have :
<%= form.fields_for :uploads, Upload.new do |uploads_attributes|%>
<%= uploads_attributes.label :file, "File:" %>
<%= uploads_attributes.file_field :file %>
<%= uploads_attributes.hidden_field :status, value: "raw" %>
<% end %>
Problem
This solution works when I want to upload only one file. But if I want to upload two files I can't figure out. I can add multiple: true to the file_field but how to created multiple Upload with one file each ?
Should I save the uploaded files into a temp variable first, extract them from the intervention_params, then create the Intervention without any Upload, then for each uploaded files saved, build a new Upload for the newly created Intervention ?
I have done what I have proposed. I don't know if there is a more elegant way to do it but anyway, this is working.
In my form I just added a simple files field that can take multiple files
<div class="field">
<%= form.label :files %>
<%= form.file_field :files, multiple: true %>
</div>
I have added this to the permitted parameters :
params.require(:intervention).permit(:user_id, :comment, files:[]])
Then I create my Intervention by ignoring this files params and I use it later to create a new Upload record for each files submitted.
# First create the intervention without the files attached
#intervention = Intervention.new(intervention_params.except(:files))
if #intervention.save
# Then for each files attached, create a separate Upload
intervention_params[:files].each do |f|
upload = #intervention.uploads.new()
upload.file.attach(f)
upload.save
end
end

Get file and filename from carrierwave

I successfully uploaded a file to S3 and now I want to attach file to email, so i can send it by using Gmail API. Some people suggested to call method .file directly like here
as you can see, you can call method
<%= #page.form.file.filename %>
.file directly from form.
here is my mount uploader class:
class Crm::LogEmailAttachment < Asset
belongs_to :owner, class_name: 'Crm::LogEmail'
mount_uploader :data, Crm::LogEmailAttachmentUploader
mount_base64_uploader :data, Crm::LogEmailAttachmentUploader
# default_scope -> {order("assets.order")}
def to_s
self.data.try(:file).try(:filename)
end
end
and here is my erb form:
<div class="document-wrap">
<%= log_email.fields_for :crm_log_email_attachments do |a| %>
<%= a. file_field :data, input_html: {class: "form-control" , wrapper: 'field'}, label: false %>
<% end %>
</div>
then I try to get file by just calling .file from params[:data] it returns undefined method file for #<ActionDispatch::Http::UploadedFile:0x007fc337322290>, my question is how can I get the path of the file and filename from carrierwave ?
Instead of referencing the params ActionDispatch::Http::UploadedFile object, you should save the data to S3 via carrierwave first, then reference the uploader object directly. For instance
object.data.file.filename
To retrieve the filename, and
object.data.url
To retrieve the S3 URL for the associated asset.

Carrierwave multiple image upload file_field is not enabling selection of multiple images in rails

I am having problems enabling selection of multiple images in implementing Carrierwave multiple images. When I click on the upload button in the new view, I just get the file selection window for a single file and can only select a single file.
In the input form, I have:
<%= simple_form_for #car do |f| %>
...
<%= f.file_field :pictures, multiple: true %>
...
In the Car model, I have:
mount_uploader :pictures, PictureUploader
serialize :pictures, JSON
In the cars controller, I have:
params.require(:car).permit(:name, :make, :year, :color, :seats,
:location, :transmission, :price, :photo, :photo_cache, {pictures: []})
I have a pictures column in the Cars table. I have include cloudinary::Carrierwave in PictureUploader.
Is there anything I'm missing?
At this time multiple image uploads are not supported with Cloudinary’s GEM and Carrierwave integration. However it is on Cloudinary’s road map of implementations. As a workaround for the time being feel free to reference this sample project for multiple uploads using Cloudinary’s GEM and Carrierwave: https://github.com/taragano/Cloudinary_multiple_uploads

How to validate sizes of multiple image uploads in CarrierWave uploader

I have my uploader set up for multiple image uploads like so:
class User < ActiveRecord::Base
mount_uploaders :avatars, AvatarUploader
end
and in view file:
<%= form.file_field :files, multiple: true %>
The issue is that the result will be in a json string of image_urls.
How do I then validate the size of each individual image i have uploaded?
For example, each of the images should be less then 5.megabytes.
Thank you very much!
EDIT:
This is based on CarrierWave(0.11.0)'s new feature thanks to Gen.
How to: Validate attachment file size
And there is no such thing as mount_uploaders:
Undefined method `mount_uploaders'

Rails: How to download a previously uploaded document?

I need my user to upload documents such ad pdf and txt on their profile. And I did that using Carrierwave, so I have a list of documents with titles and urls.
But how can I make other users download those files? Do I have to use a gem or is there something native I don't even know since I am very new to rails?
Thanks
EDIT:
society.rb
class Society < ActiveRecord::Base
...
has_many :documents, :dependent => :destroy
end
document.rb
class Document < ActiveRecord::Base
belongs_to :society
mount_uploader :url, DocumentUploader
end
And then this in the view I want to download files:
<% #society.documents.each do |doc| %>
<%= link_to "Download it", doc.url %> //url is the column name of the saved url
<% end %>
I don't use Carrierwave but I guess it's similar to Paperclip.
Therefore you should be able to use something like this
link_to 'Download file', user.file.url
This is assuming you have a user instantiated object from a Model with a 'file' carrierwave attribute. Replace this with your attribute name.
You can use the send_file method for this. Which could look like:
class MyController
def download_file
#model = MyModel.find(params[:id])
send_file(#model.file.path,
:filename => #model.file.name,
:type => #model.file.content_type,
:disposition => 'attachment',
:url_based_filename => true)
end
end
Check the apidock link for more examples.
Or you can put anchor tag:
in your controller:
#doc = "query to get the document name from your database"
#docpath = request.base_url+"/uploads/"+#doc
in your view:
<a href="#docpath" download>Download File</a>.

Resources