Rails, Upload and Parse Text File to Database - ruby-on-rails

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?

Related

Uploading/playing mp3 files using Rails

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

Rails upload excel file, validate and save

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.

Rails: Upload text file, preview, then save

I want to upload text files that have several hundred lines in it. I want to present users a preview of the first 10 lines of the file, where they can adjust a format and then click process. After the click, I want to process the file based on the specified format and save the entries to the database. Finally the file should be deleted on the server side.
How could I do that? Is there a gem like paperclip for images to handle text files?
Take a look at Ryan Bate's this cast: http://railscasts.com/episodes/396-importing-csv-and-excel

Rails app - send_data sending garbled documents from DB

I am using a rails app to display data that is stored in a sql server db. Data entry is all done through an MS Access front end into the sql server db. The rails app is "read only" viewer of information entered via the access app.
In the access front end, I use a bound object frame to let users upload documents of various types (.pdf, .msg, .docx etc.) into the database. The object is stored in a varbinary field on my sqlserver. This works fine when using the Access front end. Docs are properly retrieved and displayed with the right host app.
The problem is when sending those same objects back to be viewed through the rails app.
When I try to send the binary data down through the browser the file as a file, the file received is unreadable by its host app. The file extension is correct for the file sent down, but the applicaiton cant' seem to read them. I'm definitely getting something coming down -- when I examine the files in notepad I see snippets of the text, but something is wrong with the files so they are unreadable by MS Word or PDF Reader, etc.
The code in the controller is pretty simple:
def view_word_doc
a=Attachment.find(params[:id])
send_data a.Document, :filename => 'flibber.docx'
end
(Document is the field int he table that has the varbinary)
This has the expected result in the browser, I am asked if I want to open or save the file, and it attempts use word to open the doc. However word says the document is corrupted. Similar results when I try to open .pdf, .msg files etc.
I also tried using send_file. In that case I get a controller error --
Encoding::UndefinedConversionError in AttachmentsController#view
"\x81" to UTF-8 in conversion from Windows-1252 to UTF-8
I have left the type to default, which I understand is Application/Octet-stream but I'm not so familair with what this means.
As I mentioned, this all works fine with the access front end. Just can't send it properly in rails.
It is possible that you have coded to receive the files as a GET method in your routes.rb so it attempts to display them inline/in the browser or potentially a link used in the view whereas it needs to be a button which uses the POST method by default.

Upload and parse an Excel file in a Backbone.js app with Rails backend

There is a lot of information in the title of the question !
I'm aware of Rails gems to upload files, parse Excel, etc. but I would like to know which solution will work nice in a Backbone app (that is, I don't want to reload the page during the upload). Is there a Backbone extension for that ?
The story is: the user wants to upload an Excel file, she browses her filesystem, selects the file, click "upload"... and then the Backbone app shows the content of the file (I don't need to store the file on the server).
Well, i dont think there is any backbone extension for that :) What you need is some upload extension like uploadify of http://blueimp.github.com/jQuery-File-Upload/ . You will bind the
success method to uploader and then you can do anything with the data returned from server.

Resources