[RAILS] Image upload

Image upload tool

ActiveStorage Gem that can implement file upload function

ImageMagick Software that can perform image processing from the command line You can create images, change the size, and change the save format. To handle the software ImageMagic with Rails, a gem called MiniMagic is required.

MiniMagick Gem for using ImageMagic features in Ruby

ImageProcessing Gem that adds the ability to adjust the image size Add image size adjustments that cannot be done with ImageMagic alone.

Install ImageMagick

Install from Homebrew Terminal

% brew install imagemagick

Installation of MiniMagick and Image Processing

Added to the bottom layer of Gemfile

Gemfile


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

Terminal

% bundle install

Install Active Storage

Enable Active Storage

Terminal

% rails active_storage:install

Executing the above command will generate a migration file related to Active Storage. Don't forget to migrate.

Terminal

% rails db:migrate

association

has_one_attached A method for creating a one-to-one association between records and files

model.rb


class model< ApplicationRecord
  has_one_attached :file name
end

Strong parameters

Add the file name described above to the strong parameter.

Recommended Posts

Image upload
Image upload using CarrierWave ~ Rspec test ~
About image upload of jsp (servlet)
Upload Rails app image file to S3
Multiple image upload function using Rails Carrierwave
Image file overlay
Image preview function
Image posting function
JavaFX8 image display
[Implementation procedure] Implement image upload function with Active Storage
About image upload of jsp (servlet)
Image upload