One of the exciting things about React is the easy implementation of image uploads.
Active Storage allows your program to upload files to storages services including:
- Amazon S3
- Google Cloud Storage
- Microsoft Azure Storage
It executes the task by attaching the file to the desired object.
To get access to this feature, run
rails active_storage:install
This command creates migrations with these tables: active_storage_blobs
, active_storage_variant_records
and active_storage_attachments.
Don’t forget to run :
rails db:migrate
Update your params method within your controller with the appropriate attribute that is set in your model.
You can attach files to records by entering macros like “has_one_attached”, to map between records and files.
Here’s the example provided by https://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-files-to-records
class User < ApplicationRecord
has_one_attached :avatar
end
#controllerclass SignupController < ApplicationController
def create
user = User.create!(user_params)
session[:user_id] = user.id
redirect_to root_path
endprivate
def user_params
params.require(:user).permit(:email_address, :password, :avatar)
end
end