Upload Files to a particular Folder in Ruby On Rails 4 - ruby-on-rails

I am using working on a particular module in Ruby on Rails, where I want to upload files to a particular folder. If it's possible, would you please share some examples which may help me achieve this functionality?

As #dharam mentioned. you can use paperclip. The working example you can find here
demo
If you want to specify folder path where the attachments should move. you need to write this in your model
has_attached_file :attachment, :path => ":rails_root/attachments/:id/:style/:basename.:extension"
After this all your attachments will be seen in attachments folder in your Application root

paperclip gem supports multiple storage options, including file storage.
Refer to FileSystem for more info.
Other gem you can look into is carrierwave

Related

Rails file upload: upload a folder

I work on Rails project and client asked if I can add 'upload a folder' feature to simple file upload system that we have now. Currently it attaches files to model and then displays them on a page for download. Pretty basic.
But I can't figure out how can I handle folder uploads, with every folder having it's own content. Is there any pre-made gems that can help accomplish that?
We use Paperclip at the moment, but I don't mind migrating to Carrerwave or some other gem that would
UPDATE I see that I was unclear about my needs. I need an upload system that could handle folders. Something like this.
In Dropbox I am able to upload both files and folders. How can I make my uploaders accept folders and then display them alongside regular attached files?
you can solve it by using the interpolation of paperclip where you can create or naming the folder dynamically for the same you need to do like below
specify the path into model which you wanted always
:path => ":folder/:id_:filename"
and specify the private method in same model or using globally specify in initializer
Paperclip::interpolates :folder do |attachment, style|
attachment.instance.name
end

Upload file in Rails 3.2 app - develop engine or use gem?

We are looking to add a simple file uploader to our rails 3.2 app which is a business application (with Rails engines). Here are what we are looking for with the file uploader:
Allow access control to who can do what. For example, sales can upload a contract and acct can view the uploaded contract.
No change to current model. The file uploader acts on its own about file uploading, checking, storing and removing. We are thinking to have a file uploader engine and attach the engine to the Rails app.
File uploaded belongs to a model. For example, uploaded contract copy belongs to a project.
May need to upload file to a remote server.
We are evaluating options of developing our own uploader engine or find a upload gem such as carrierwave or paperclip. Can someone shed a light on rails file uploading and its related issue?
Using a combination of cancan and paperclip is a good option.

Rake task creates CSV and then upload to S3. How?

I have a rake task that creates a CSV file. Right now I am storing this file into my /tmp folder (I am using Heroku). This CSV file is not associated with any model, it's just some data I pull from several APIs, combined with some data from my models.
I would like to download this file from Heroku, but this seems not possible. So, my question is: Which gem am I trying to look for in order to upload that file to Amazon S3? I have seen gems like Paperclip, but that seems to be associated with a model, and that is not my case. I just want to upload that CSV file that I will have in /tmp, into my Amazon S3 bucket.
Thanks
You can use aws-s3 gem
S3Object.store('filename_in_s3.txt', open("source_file.tmp"), 'bucket_name')
You should define the exact path of your tmp file, for example:
open("#{Rails.root}/tmp/source_file.tmp")
CarrierWave can directly interface your Ruby application with S3 directly via the Fog library. Rather than operating on the model level, CarrierWave utilizes a Uploader class where you can cull from across your APIs and datasources, which is precisely what you're trying to accomplish.

How should I parse a file in Ruby on Rails to store in a database?

I want to create a simple form for users to upload a file, which will be stored in a database. I also want to display all the submitted files in the database with their name and a link to download the file. Schematically, what's the best way to do this in Rails/how should I store the file in table (which fields should my table have? etc). Thanks!
i would use paperclip gem with the upload to s3 instead of file system
https://github.com/thoughtbot/paperclip
checkout the README, most of the examples are for an image, but works with non-image files as well
use paperclip to upload file, you can store images/file in your database as well as in s3 (AWS)
See below link how to use paperclip in rails with example
Here is the steps how to upload file using paperclip in rails
http://patshaughnessy.net/2009/4/30/paperclip-sample-app
for github
https://github.com/thoughtbot/paperclip
https://github.com/websymphony/Rails3-Paperclip-Uploadify

Uploading/Downloading files - Ruby On Rails system

I'm attempting to create a simple file hosting system using Ruby On Rails - I have a large ammount of the system setup (including the registration of new files, and stuff) however I've realised there is a bit of a problem - I'm unsure how to actually get it so that users can upload and download files.
I assume I'd need some kind of file_link attribute for my file object, but how would people upload and download files to/from the server?
Also (this may be a slightly different topic) - but how would I get the file information such as file size and name (as I need them for the upload)?
Sorry for all my questions - I've don't really deal with file handling a lot so am new to the area.
Thanks In Advance,
Regards,
Joe
You should look at Paperclip gem https://github.com/thoughtbot/paperclip
It is very easy to use and allows to upload files.
Look at Paperclip. It does a lot of the heavs lifting for you: https://github.com/thoughtbot/paperclip
As they said look at paperclip. I just did a app that allows the users to upload and delete files. To get started with paperclip use http://railscasts.com/episodes/134-paperclip
To download files after uploading them with paperclip. I did the following in the controller
def download
upload = Upload.find(params[:id])
send_file upload.uploaded.path,
:filename => upload.uploaded_file_name,
:type => upload.uploaded_content_type,
:disposition => 'attachment'
flash[:notice] = "Your file has been downloaded"
end
My sample file upload app should be of help https://github.com/skillachie/File-Upload-App
Need to fix a few things , but the ability to upload and download files is completely functional.

Resources