I'm working on my first "real" Rails project - "modernizing" an old website for a church choir and am building it in Rails. They currently have a practice page with a list of links to mp3 files stored on their server. I would like to store the mp3 files in a database and have created a model for the songs - title:string, part:string (tenor, soprano, etc), audio:binary (this is the mp3 file). When I submit the form, the audio field is nil - the other fields save correctly. Should I be using a Gem for uploading/saving the mp3 files? I've come across CarrierWave, but have only seen it used with images. I was under the impression that the binary field in active record was for preserving the original file format. Thanks in advance!
If you want to use file upload instead of saving it into database then you can use paperclip . please have a look at it https://github.com/thoughtbot/paperclip
Related
I am working on a rails engine that uploads a excel file, validates it and if there is no error than it will save it to database.
Now when ever a user mounts the engine and than go to the route provided by engine. He will have a form to upload the excel file. There are two buttons on page, i.e, upload and validate.
Once a user choose the file and when he click on upload i want that file only gets uploaded and don't get saved in db. Once i get the message the file is uploaded successfully, than i will validate the file. If it is a valid excel file with valid data than it will be saved into db. Now i am not getting how to go about it. I have seen this Railscasts video on uploading csv and excel file but here he is performing validation and save operation with import action but i want validation and save operation when user clicks on validate action. This Questions seems similar to my problem but i am not getting how do i access that uploaded file. I don't want that file to be saved in database. I mean when a user click on upload button that file gets only uploaded not saved. Than i will validate that file and save it's content to db.
This may seem very easy and simple questions for some experts but i am very new to rails and i am not sure how to go about it.
Someone please help me with a sample code, so that i can understand the workflow. Also note that both upload and validate actions are on same page. So when a file gets uploaded it needs to be stored somewhere temporarily, this is the first problem i am facing. I can do all the task if someone can tell me workflow with a sample code about uploading excel file. I am only having problem here that as both upload and validate action are on same page, so after upload request it needs to be on that page so that i can validate that file.
Any help would be appreciated, I am very beginner at rails and really confused here.
Two options:
Write code to upload the file and save to DB with a validated column set to false. Then the 'validate' button will locate the unvalidated file, validate it and set validated to true. You could have a periodic job deleting unvalidated files of a certain age. If you do this, use a helper gem like Paperclip.
Forego file upload frameworks and just manually save uploaded files to Tempfile.new 'spreadsheet'. This guide takes you through how to do that. Save that filename to session and use it to validate at a later point. When you're finally ready to persist to DB, again, consider using a helper gem.
My website generates a file in javascript (audio recording) and I then want it to be uploaded to Amazon S3.
I first managed to get the uploading part working by sending the generated file to my server, where it is uploaded. However I would like now to upload the file directly to S3, without going through my server.
So I started to use the s3_direct_upload gem, which works great when using a file_field. However my file is generated by the javascript and :
- The value of a file field has to be set by the user for security reasons
- I do not want the user to have to interact with the upload
I tried to play with the S3Uploader class and to directly add data, without any success for now, it seems that I do not use the correct method.
Does anyone has any idea on how to achieve S3 direct upload without a file field ?
Thanks
Never mind, I found out that the S3Uploader class used by the s3_direct_upload gem has the same methods as the jQuery-File-Upload from which it is derived.
So one can call $("#s3_uploader").fileupload('send', {files: [f]});
And the f File will be uploaded to S3 directly
I have an advertising site running where users can post "headlines" and "sublines" in a form. I want to take this thing further.
Users should upload their .txt or .rtf file, comma seperated, so that it's created in my database.
Let's say this is the users rtf or whatever:
This is my headline; This is my subline;
This is my second headline; This is my second subline;
How can I achieve that this is parsed and written to my database? No csv or whatever. Just a simple text file.
Where should I put this form?
How can I parse it?
You might want to look at using Paperclip to upload the file & then you'll be able to access its data using this answer: How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?
I hava a Ruby on Rails application that works with video playlists. Now I would like to extract timecode information from the video file without uploading it to the server (takes to long). Is the possible?
If it is not possible, is there a way to export te metadata locally and uploads these xmls (for example) to the server?
Thanks in advance
afaik, this is not possible in browsers that do not support File API. Take a look at jquery file upload and the way it allows user to identify the file extension before upload.
I'm interested if it is possible to attach already uploaded files to new activerecord instances? For example I have a lot of pictures uploaded and want to choose one of them. Is it even possible using paperclip? I know Ckeditor can handle it somehow when used with activeadmin.
You can pass a local path to the file to trigger storage processing from somewhere accessible to the server.
User.attribute_that_is_attachment = File.new('/local/path/to/file.txt')