For the longest time I just wanted a quick, ready-to-go example of how to do some basic file uploads in a real rails app… Well, finally I wrote it.
Welcome to TinyFile. An upload and file management tool supporting uploads, overwrite prevention, deleting, and renaming all in 51 lines of code. There was a friendly goal of keeping it around 50 lines just to see what was possible. Some things are a little tight, but everything should still be readable.
It comes bundled with a sqlite database to get you running fast.
http://www.dwgsolutions.com/tiny_file.tar.bz2
Installation Instructions for Windows:D:\RubyD:\Ruby\wwwD:\Ruby, copy the files to D:\Ruby\bintiny_file.tar.bz2 and extract toD:\Ruby\wwwThis schema worked for me for playing with this code.
CREATE TABLE resources ("id" INTEGER PRIMARY KEY NOT NULL, "path_to_file" varchar(255), "filename" varchar(255));
NOTE: be sure to add “auto_increment” to the ID field of the table. Otherwise, Rails will just attempt to insert a “0” into the ID field each time. So my working SQL script is:
DROP TABLE IF EXISTS resources; CREATE TABLE resources ( id INTEGER PRIMARY KEY auto_increment NOT NULL, path_to_file varchar(255), filename varchar(255) );
(and BTW, thanks for simple code to get me going with file uploads!)
Question: I downloaded the code and run the script, when I attempt to upload a duplicate file I am able to upload it, with a number appended to the end of the filename: filename.jpg2 for example. I believe this has to do with attempting to copy the file to the upload directory before setting the filename field but I can’t figure out how to fix it.
Question: I also downloaded this and when i ran it i get the following error and ive downloaded the right files to my ruby/bin directory, and windows\system32. The error i get is “This error occured while loading the following files:
sqlite”
Im new at Ruby so please can you help, thanks. Should i be asking this kind of question here or is there a forum?
This error is happening because you don’t have SQLite installed. This can usually be corrected by running “gem install sqlite” from the command line. Keep in mind that there are two branches of the SQLite code which are incompatible with each other, ‘sqlite’ and ‘sqlite3’. Since this comes with a SQLite 2.x database, ‘sqlite3’ won’t work with this.
This is the preferred place to ask such questions since it will help more people, but you’ll probably get a faster response from a forum.