Best approach to multiple file uploads in Ruby on Rails - ruby-on-rails

I have asked a similar question here with unsuccessful answers:
Uploadify + Paperclip + Rails nested association before_save
So i will reformulate my question:
What's the best approach in Rails to upload multiple files at once and associate them to an object that is not yet saved? (for example a model (girl) application form that is saved to the database when the create action is been complete (the save button is pressed).
Many ideas come to my mind (save the object by ajax before he try to upload the images, add a token to images and then add the ID of the model after the model object is saved) but im pretty sure many people have done this and there's a common way or best approach to do it.
Thank you in advance!
Martin.

I use this with one of my rails 3 apps:
= form_for :import_csv, :url => {:action => :import_csv}, :html => {:multipart => true} do |f|
= f.file_field :csv
= f.submit 'Upload CSV'
This creates a temporary file which can be retrieved with
CSV.open(params[:import_csv][:csv].tempfile.path)
I see no reason why this could not be extended to multiple uploads, and accessed at params[:import_csv][:whatever]
Note** the handling of tempfiles was changed slightly in rails 3.0.3, which is why the above code uses .tempfile.path which was not required in previous versions.

Over a year ago I was faced with a similar problem and could not find a ready solution therefore has made as follows:
1.Using SWFUpload upload images to an "store_image" action that stores, resizes, ..., and returns path to the thumbnail and the IDs of uploaded image.
2. Using JS put image IDs in hidden field(s) value. I used single field with value like "2312111:3231231:323212".
3. When create a "master" object, find the uploaded images by their IDs and establish their relation with the subject.
Also garbage collector removes unrelated images created over 3 days ago. The garbage collector runs by cron every day.
As for me, it is the most elegant solution.
__
Sorry for my bad English

Related

How to add new attachinary image to an instance instead of replacing the whole array in Rails 5

This might be a stupid question since I can't find it already asked here but I can't find a solution no matter how I tried.
I use Rails 5 with Cloudinary and Attachinary gems.
I have a model PointOfInterest with
has_attachments :photos, maximum: 3
Everything works fine if photos are uploaded at once (with simple_form). But I want my users to be able to upload photos sequentially ; for instance 1 photo at the creation of the point_of_interest and then add 2 more later on edit.
Unfortunately the update method deletes the attachments before saving the new input.
I ended up building an array with the old attachment and new one from the update and tried to overwrite the attachments:
#point_of_interest.photos = new_photoset
but I get a Runtime error => Can't modify frozen hash.
I also just realized my new_photoset array mixes Attachinary::File (previous image) and Attachinary::File::ActiveRecord_Associations_CollectionProxy (new image) so I'm pretty sure I'm not on the right track.
I tries many different approaches and I don't find the Attachinary docs very helpful.
Or maybe I should investigate on the jQuery-File-Upload side?
Any help will be much appreciated.
I would suggest you split the actions in two steps. Add to your model:
has_attachment :point_of_interest_picture
and then step one would be to have the user upload a picture, and step two would be to upload two or more pictures

Multi file uploads in rails 4

I'm having a few problems with multi file uploads in rails 4, I've got it working using carrierwave and polymorphic associations but its creating a file filed for every image in the view. This makes sense as its creating fields for the associated model.
If there is a better or more standard way of doing this in rails 4 I'd be grateful for your advise.
Here is a gist to what I'm doing: https://gist.github.com/lperry65/5805b5f41495f8a820b7
Screen Shot
http://cl.ly/image/3y3w203z3G1f
There seems to be lots of ways to accomplish this, but most of the info I found while googling is for rails 3. It's my intention to use UploadiFive for the front end once I iron out the wrinkles.
http://www.uploadify.com/download/download-uploadifive-standard/
Changing <%= f.fields_for :images do |i| %> to <%= f.fields_for :images[0] do |i| %> stopped the file input from being duplicated for every image. I can't say I'm happy with it but it works for now.
I'll update the gist to reflect any other changes, it's all working now so I'll leave it until I find a better solution.

Rails ActiveRecord associations with bulk image uploads

I have a model Item that has an attribute :code.
Items are added to the database via CSV file uploads in rails. Each :item should have a product image associated with it.
The research I've done so far seems to suggest that bulk image uploads (think 500-1000 images) are best handled outside of rails.
My question is this: if I upload bulk images to S3, is there any way to associate images to their respective :item? For simplicity, let's assume that we can easily infer :code from each images filename.
The end goal is to display an items image with something like:
<%= image_tag("#{#item.image}") %>
Let me know if I can clarify, thank you!
You gave a little to few information (e.g. what gem do you use to store the images) for an optimal answer. So here is the answer for the given question: Add the following method into your Item model:
def image
"example_image_#{self.code}.jpg"
end
Please be aware that image_tag() will always result into an asset pipeline path. Please see http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

How to keep temp files over requests in Rails?

In my Rails application users can upload Excel files. In my model there is a class ImportFile that uses attachment_fu like this:
class ImportFile < ActiveRecord::Base
has_attachment :storage => :file_system, :path_prefix => 'public/imports', :max_size => 10.megabytes
end
When user clicks "Add file" he enters a page with a <%= fields.file_field :uploaded_data %>. attachment_fu does it's job and file upload is being done (let's ommit validation problems). I want to keep this file for future so I copy uploaded file to other temp file. Temp file is working fine - I can see it on disk.
def self.write_to_tmp(data)
temp_file = Tempfile.new("import", "#{Rails.root}/tmp")
begin
temp_file.write(data)
ensure
temp_file.close(false)
end
temp_file
end
What I want to do is to show user a preview and then let him choose if he wants to add a file or discard it - there are two buttons. I have a problem when user chooses to save a file, because a temp file I've just created above is gone. It is deleted before request.
Does anyone has hints how to achive this? Or can point me to upload-with-preview file scenario like the one I've presened? I've been looking for days, but I've failed to find one.
The most reliable approach to this sort of thing would be to create a simple "upload" tracking model like you have there, but using Paperclip instead. This can be configured to handle a very large number of files.
You need to actually save these records for them to persist between requests. This will lead to orphaned records, but a simple cron job or rake task you can kill off all the unused files any time you need to.
Creating a large number of files in a single directory is usually a bad idea. Paperclip has a path parameter which will split up your ID number into parts, so record #903132 goes into .../90/31/32 for example.
Keep a regular attachment, and if they want to discard it, delete it, otherwise use it. At some point later clean out all the unused attachments.

way of implementing links in rails

I have a project I am doing in rails. I want to implement this sort of links
{user} has {action} on {file} in {project}
each of the words wrapped by curly braces are entities in my system (models). how do I implement saving these changes in the project and how do I get all of the changes from the database and display them to the user?
I am using rails 2.3.8 if it matters
example of the links I need to display (image)
There are some plugins available to keep track of activities in models:
https://github.com/grosser/record_activities
https://github.com/linkingpaths/acts_as_scribe
https://github.com/face/activity_streams
I normally use acts_as_scribe, it's the simplest of them.
I would recommend acts_as_audited. It works very well. It saves all your changes on a model in the form of a hash, so using it becomes as easy as
audit = Audit.first #just for example
In your view
<%= link_to User.find(audit.user_id), user(:id => audit.user_id) %> has
<%= audit.action %>
Of course you will have to customize how your messages will finally appear. And of course its better not to use find methods in your view. I've used it here just for illustration purposes.

Resources