The Rails 6 File Upload is a small task for everyone to practice more when programming with Ruby on Rails.
We will use gem carrierwave to perform this task.
Support this project 😜 🙏
You can try it at https://uploadfilerails6.herokuapp.com/
- Ruby on Rails
We are going to build the web application using:
- Rails 6.0.3.2
- Ruby 2.7.1
Ability to upload many different types of files: .jpg, .png, .pdf, .mp4 , .mp3, .word, .excel, .....
$ git clone https://github.com/TanHongIT/upload_file_rails_6
$ cd upload_file_rails_6
$ bundle install
$ yarn install
You must change the appropriate database configuration
Change configuration at "config/database.yml" with Postgresql.
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
username: upload_file_rails_6
password: 1234
host: localhost
# tutorial for ubuntu linux:
# sudo -u postgres psql
# create user "upload_file_rails_6" with password '1234';
# create database "upload_file_rails_6" owner "upload_file_rails_6";
development:
<<: *default
database: chat_room_rails_6
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: upload_file_rails_6_test
production:
<<: *default
database: upload_file_rails_6_production
You must change the username, password and database name accordingly!
$ rails db:migrate
$ rails s
And now go to http://localhost:3000/
rails new upload
cd upload
gem install carrierwave
gem install bootstrap-sass
gem 'carrierwave'
gem 'bootstrap-sass'
bundle install
rails g model Resume name:string attachment:string
rake db:migrate
rails g controller Resumes index new create destroy
rails g uploader attachment
class Resume < ApplicationRecord
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
validates :name, presence: true # Make sure the owner's name is present.
end
resources :resumes, only: [:index, :new, :create, :destroy]
root "resumes#index"
class ResumesController < ApplicationController
def index
@resumes = Resume.all
end
def new
@resume = Resume.new
end
def create
@resume = Resume.new(resume_params)
if @resume.save
redirect_to resumes_path, notice: "Successfully uploaded."
else
render "new"
end
end
def destroy
@resume = Resume.find(params[:id])
@resume.destroy
redirect_to resumes_path, notice: "Successfully deleted."
end
private
def resume_params
params.require(:resume).permit(:name, :attachment)
end
end
@import "bootstrap";
<!DOCTYPE html>
<html>
<head>
<title>UploadFileRails6</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
<div class="container">
<% if !flash[:notice].blank? %>
<div>
<%= flash[:notice] %>
</div>
<% end %>
<br>
<%= link_to "New Resume", new_resume_path %>
<br>
<br>
<table border="3">
<thead>
<tr>
<th>Candidate Name</th>
<th>Download Link</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% @resumes.each do |resume| %>
<tr>
<td><%= resume.name %></td>
<td><%= link_to "Download", resume.attachment_url %></td>
<td><%= link_to "Delete", resume, method: :delete, confirm: "Are you sure you want to delete #{resume.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="container">
<% if !@resume.errors.empty? %>
<div>
<ul>
<% @resume.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %></ul>
</div>
<% end %>
</div>
<div class="container">
<% if !@resume.errors.empty? %>
<div>
<ul>
<% @resume.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %> </ul>
</div>
<% end %>
<div>
<%= form_for @resume, html: {multipart: true} do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br><br>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<br>
<%= f.submit "Save" %>
<% end %>
</div>
</div>
rails s
You can change the path to save the file at the store_dir function in the file "app/uploaders/attachment_uploader.rb"