Active Storage Initialization Manual

Introduction

Thing you want to do

--I want to easily register images

What you can do with Active Storage

--Easy to associate images with models --It is possible to update to cloud storage such as Amazon S3 and GCP => Certainly, when deploying on AWS, you shouldn't have to mess around with Active Storage. .. ..

Try it

I am indebted to you every time, but I will forget the initial setting method every time, so I will make a note of it.

Active Storage is a ** Gem ** for file uploads. Originally it was a Gem that needed to be installed externally, but now that it's integrated into Rails, you don't need bundle install for Active Storage itself.

Reference URL

Rails Guide> Overview of Active Storage

procedure

--Introduction of image processing tools --Active Storage installation and migration --Association --Include in strong parameters --Display the saved image

Introduction of image processing tools

Install ImageMagic

What is ImageMagick?

One of the image processing libraries. It's software, not Gem, so install it from Homebrew. Therefore, once it is built, this work will not be necessary from the next time.

Terminal


% brew install imagemagick

Installation of required Gem

MiniMagick

Gem for handling ImageMagick in Ruby

ImageProcessing

Gem for image size adjustment

Gemfile


gem 'mini_magick'
gem 'image_processing', '~> 1.2'

Terminal


% bundle install

& Server restart

Active Storage installation and migration

Since Active Storage is a Gem to the last, install it.

Terminal


% rails active_storage:install

When the installation is completed, an Active Storage related migration will be created, so migrate as it is.

Terminal


% rails db:migrate

association

Describe the association in the model you want to handle the image.

models/profile.rb


class Profile < ApplicationRecord
  has_one_attached :image
end

At this time, it is not necessary to provide an image column in the target table.

Strong parameter settings

Finally, include the image in the strong parameter.

profiles_controller.rb


class ProfilesController < ApplicationController

~ Omitted ~

  private
  def profile_params
    params.require(:profile).permit(:image)
  end
end

Now you can save the image data.

Display the saved image

Display using the image_tag method.

example


image_tag model.Column name
image_tag profile.image

Conditional branch depending on the presence or absence of an image

If you want to change the display with or without an image file, use the attached? Method.

example


image_tag profile.image, class: 'profile-image'  if profile.image.attached?

*** * Write an article about adjusting the size of the image! *** ***

in conclusion

Qiita, this is my second post. I will update it every day as much as possible until I run out of material.

Thank you for reading until the end! !!

✔︎

Recommended Posts

Active Storage Initialization Manual
Introduce Active Storage
About Active Storage
[Beginner] About Active Storage
Post videos with Active Storage
Move Active Storage on S3 locally
[Rails] How to use Active Storage
Unit tests under Active Storage deployment
[Active Storage] Validation settings when uploading files
Rails Active Storage shrinks images before uploading
Post / delete multiple images with Active Storage