[RUBY] [Rails] How to use Active Storage

Introduction

With Active Storage, you can use methods that make it easy to upload files such as images, and you can easily create tables to store images.

table of contents

  1. Introduction of image processing tools
  2. Install Active Storage
  3. How to save the image
  4. Image display

1. Introduction of image processing tools

First, we will introduce the image conversion tool required for image processing and the gem to make it available from Rails. ImageMagic allows you to create, resize, and change the save format of images from the command line. Execute the following command.

Terminal
brew install imagemagick

ImageMagic is not a gem, so to handle it with Ruby or Rails, you need to install a gem called MiniMagick. MiniMagic is a gem that allows ImageMagic to be used in Ruby. Next, install the gem to change the image size. Image Processing is a gem that provides a function to adjust the image size, which is not possible with MiniMagic. Add it to the bottom of the gemfile as follows. Then run bundle install with the command.

gemfile


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

2. Install Active Storage

Do the following in your terminal:

Terminal
rails active_storage:install

Now that the migration related to Active Storage is created, do the following:

Terminal
rails db:migrate

3. How to save the image

Set the association definition and save permission for the image column to store images in Active Storage tables. This time, I used the has_one_attached method to attach one image file.

app/models/post.rb


class Post < ApplicationRecord
  ~Abbreviation~
  has_one_attached :image
  ~Abbreviation~
end

The image file is now linked to the posts table. ** At this time, you don't need to add any columns to the posts table. ** **

Then add an image to the strong parameter to allow the image file to be saved.

app/controllers/posts_controller.rb


class PostsController < ApplicationController
  ~Abbreviation~
  def post_params
    params.require(:post).permit(:name, :image, :price, :evaluation, :description, :category_id, :shop_name).merge(user_id: current_user.id)
  end
  ~Abbreviation~
end

4. Image display

Display the image using the image_tag method, which is a Rails helper method.

Example
<%= image_tag post.image, class: "post-img" %>

You can use the attached? method to prevent errors when the image does not exist.

Example
<%= image_tag @post.image ,class:"user-box-img" if @post.image.attached? %>

By writing as above, image_tag will be loaded only when the image exists.

If you have Active Storage installed, you can use the variant method. You can specify the display size of the image file by using the variant method.

Example
<%= image_tag post.image.variant(resize: '500x500'), class: "post-img" %>

Recommended Posts

[Rails] How to use Active Storage
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to use Ruby on Rails
[Introduction to Rails] How to use render
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
How to use MySQL in Rails tutorial
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[rails6.0.0] How to save images using Active Storage in wizard format
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
How to link images using FactoryBot Active Storage
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
[rails] How to post images
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use JUnit (beginner)
How to use Ruby return