user1104783
user1104783

Reputation: 155

Storing an uploaded file's path in the database

I've been trying to do simple file uploads but I am unable to find a gem that does it, all of them just seem to be orientated around images. I just want basic .zip file uploads. Could someone instruct me to a gem that does this or a tutorial that builds your own custom uploads. However, if there are none, I'll create my own file uploading, but then I am faced with the following question

The question:

How do I store the file's path in the database when the user hits the upload button?

Below is the scenario:

The form consists of the following:

Title box:
File select box:
Upload button:

The database table just consists of two columns, the title (Title) and the file's path (FilePath) on the filesystem

The user fills in the title, browses for the file on their system, and clicks on the upload button, the file then needs to be stored on the disk, lets say /public/uploads/file1.zip while also writing to the mysql database the Title of the upload as well as the path to the actual file that resides now in /public/uploads/file1.zip.

On another page, I can simply just query the database to retrieve the Title and FilePath which will display the Title and a link/path to the file that a user can simply just click on and download.

It will be displayed on the page as follows:

Title - Download

so i.e Conference Document for the 27th - Download

Where "Title" = user submitted file title and "Download" = the actual link you can click on that will retrieve the file from the disk and start downloading. This needs to be one query from the database that retrieves the title and the path to the file. The querying part I know how to do. It's just the part of uploading the file to that directory, storing the directory for that specific file in the database and it's Title the user submitted.

Any suggestions, examples and resources will be greatly appreciated. Thanks for your time.

Upvotes: 2

Views: 2477

Answers (2)

clyfe
clyfe

Reputation: 23770

Read uploading files from the rails guides and then read the CarrierWave README. Basically it's:

# in the model
mount_uploader :archive, ArchiveUploader, mount_on: :archive_path

# in the controller action
@record = YourModel.new(params[:record]) # moves the file from server upload path to CW cache dir
@record.save # moves the file from CW cache dir to final fs dir, and stores path in mounted_on column

# in the form
<%= f.file_field :archive %>

# in the view
<%= link_to 'Download', @record.archive_url %>

The filesystem path will be stored in the table column you mount on.

Upvotes: 1

sameera207
sameera207

Reputation: 16619

As @clyfe mentioned, carrierwave is the way to upload the files, As you want carrierwave will store the file in the disk and store the path in the db.

its readme file itself has most of the details to set it up

Once you set it up right its very easy and most of all it works with rails 2.x and 3.x.

Following is a nice screen cast you may want to check

HTH

Upvotes: 0

Related Questions