Uploading a file to the server and saving the path to the database in Ruby on Rails

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

Here is the Model:

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

Here is the controller:

 class Files::DataImportsController < ApplicationController def index end def new end end 

And I have index and new tags.

I want the field to load. The data should be stored on the server and save the file path in the database in the specified file_name column. The path should be the default for all downloaded files.

I'm stuck how to get started. Please help me find a solution, and I will learn from this.

Thanks in advance.

+4
source share
3 answers

db / migrates / 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 

application / controllers / upload_controller.rb

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

application / views / downloads / 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 %> 

application / 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 

application / 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 

application / controllers / upload_controller.rb

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

application / controllers / upload_controller.rb

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

application / views / downloads / show.html.erb

 <h3><%= @file.name %></h3> <img src="<%= url_for(:action => 'file', :id => @file.id) %>"/> 
+5
source

you should use one of the solutions already available, such as paperclip: https://github.com/thoughtbot/paperclip or carrierwave: https://github.com/jnicklas/carrierwave

In addition to Readmes, there are also good tutorials:

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

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

edit: Since you want to implement it yourself, I recommend that you study the sources above on Github and try to understand what their code does. Also, I would not implement it myself, but if you have reasons, it can make you go.

+1
source

You might want to explore a solution like carrierwave .

The Github page gives a good explanation of how to use it, but this is also a good guide.

0
source

Source: https://habr.com/ru/post/1434938/


All Articles