Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading File into Server and store the path in database in Ruby on Rails

I am very new to ROR. I have a task to finish:

Here's the Model:

class File::DataImport < ActiveRecord::Base
  attr_accessible :created_by, :file_name, :file_source, :updated_at, :updated_by
end

Here's the Controller:

class Files::DataImportsController < ApplicationController
  def index
  end

  def new
  end
end

And the views I have are index and new.

I want a field to upload data. The data should be stored in the server and save the filepath into the database in a specified column file_name. The path should be default to all uploading files.

I am stuck with how to start. Please help me to find the solution and I will learn from this.

Thanks in advance.

like image 509
Sri Avatar asked Sep 18 '12 20:09

Sri


People also ask

Where are Rails files stored?

By default in the development environment, Active Storage stores all uploaded images on your local disk in the storage subdirectory of the Rails application directory. That's the file you uploaded!

What is activestorage?

1 What is Active Storage? Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.

How do I upload on curl?

Send the data using curl's –d or –data option. To execute a CURL file upload, you need to use the –d command-line option and begin data with the @ symbol. The file's name should come after the data with @ symbol so CURL can read it and send it to the server.


2 Answers

db/migrate/20110711000004_create_files.rb

class CreateFiles < ActiveRecord::Migration
def change
  create_table :files do |t|
  t.string :name
  # If using MySQL, blobs default to 64k, so we have to give
  # an explicit size to extend them
  t.binary :data, :limit => 1.megabyte
  end
end
end

app/controllers/upload_controller.rb

 class UploadController < ApplicationController
 def get
 @file = File.new
 end
 end

app/views/upload/get.html.erb

<% form_for(:file,
url: {action: 'save'},
html: {multipart: true}) do |form| %>
Upload your file: <%= form.file_field("uploaded_file") %><br/>
<%= submit_tag("Upload file") %>
<% end %>

app/models/file.rb

class File < ActiveRecord::Base
def uploaded_file=(file_field)
self.name = base_part_of(file_field.original_filename)
self.data = file_field.read
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, '')
end
end

app/controllers/upload_controller.rb

def save
@file = File.new(params[:file])
if @file.save
redirect_to(action: 'show', id: @file.id)
else
render(action: :get)
end
end

app/controllers/upload_controller.rb

def file
@file = File.find(params[:id])
send_data(@File.data,
filename: @File.name,
disposition: "inline")
end

app/controllers/upload_controller.rb

def show
@file = File.find(params[:id])
end

app/views/upload/show.html.erb

<h3><%= @file.name %></h3>
<img src="<%= url_for(:action => 'file', :id => @file.id) %>"/>
like image 190
Claud Kho Avatar answered Sep 22 '22 02:09

Claud Kho


you should consider using one of the already available solutions like paperclip: https://github.com/thoughtbot/paperclip or carrierwave: https://github.com/jnicklas/carrierwave

Besides the Readmes there are also good tutorials out there:

http://railscasts.com/episodes/134-paperclip

http://railscasts.com/episodes/253-carrierwave-file-uploads

edit: As you want to implement it yourself I recommend examining the sources of the above on Github and try to understand what their code is doing. Also I would not bother implementing it myself, but if you have your reasons this might get you going..

like image 22
tmaximini Avatar answered Sep 21 '22 02:09

tmaximini