Rails: Active Storage Basics (File Uploading)
Active Storage is now the preferred way to handle file uploads in rails. Whether you are handling images, documents or even videos Active Storage can help you.
Let's begin implementing file upload in our application
Setting up the Database Tables
Before we can use active storage in our app we need to add a couple of tables to our database.
This will generate the migration file for us. Next we just run the migration.
That's it in terms of database setup.
Add Relation to Model
Now we can add the relationship to the model we want to allow uploading. Let's add a cover_picture to the Post model.
class Post < ApplicationRecord
Enabling Cover Picture at the Controller
Since we're using strong parameter for our controller we need to add the cover_picture to the list of allowed attributes
class Admin::PostsController < Admin::BaseController
:title, :body, tag_ids: => 
Adding the File Field to the Form
In our form we need to add the file field (so we can browse and upload file)
<%= form_for [:admin, @post] do |f| %>
<%= f.label :cover_picture %>
<%= f.file_field :cover_picture %>
<% end %>
We should now be able to browser to the form and upload a cover picture.