--I want to easily register images
--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. .. ..
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.
Rails Guide> Overview of Active Storage
--Introduction of image processing tools --Active Storage installation and migration --Association --Include in strong parameters --Display the saved image
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
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
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
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.
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 using the image_tag method.
example
image_tag model.Column name
image_tag profile.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! *** ***
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