Rails File Upload
We can upload a file in Rails through file uploading. We will learn how to upload a file in this tutorial.
Let us see an example of file uploading through Rails.
Step 1 Create a Rails application called upload.
Step 2 Change your directory to upload.
Step 3 Install the following gems.
Step 4 Go to the Gemfile in your directory and add the following gems.
Step 5 Run the following command.
Step 6 Create a model with two strings as name and attachment.
Step 7 Migrate your database.
Step 8 Generate the controller file in your application.
Step 9 In this step, we will create an uploader through carrierwave gem.
Step 10 Now open the app/models/resume.rb model file and write the following code.
Step 11 Go to config/routes.rb file and write the following code.
Step 12 Go to app/controllers/resumes_controller.rb file and write the following code.
Step 13 Add bootstrap in app/assets/stylesheets/resumes.scss file.
Step 14 Go to app/views/layouts/application.html.erb file and write the following code.
Step 15 Go to app/views/documents/index.html.erb file.
Step 16 Go to app/views/documents/new.html.erb file.
Step 17 Now start the server.
Step 18 Run this link on your browser http://localhost:3000/
The following page will appear in front of you.
Click on New Resume.
Click on Save button. Your document will be uploaded.
To download this document, click on Download link.
To delete this document, click on Delete link.
DownloadDownload this example