[Rails 6] Add images to seed files (using Active Storage)

Introduction

When I wanted to add an image to the seed file, I was worried because I didn't know how to write it, so I will describe it below. This is written by a beginner, so please point out any mistakes.

When dealing with images with rails, I think that you often use Active Storage or Carrier Wave. Reference: ActiveStorage vs CarrierWave

This time, we are using Active Storage, so please proceed only if you are using it.

Put the user's initial data in the seed file

We will describe the initial data of the user created using devise in the seed file.

The columns in the user model look like this.

db/schema.rb



    t.string "name", null: false
    t.text "profile"
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "avatar"
    t.boolean "admin", default: false

In addition to the columns included in devise by default, avatar is added as a column for user images. (In addition, name, profile, and admin are added.)

% rails s

Please check if you can register the image from the browser.

If you can add it without any error, let's write it in the seed file.

db/seeds.rb


user = User.create(name: "Saya", 
                  profile: "It is an office lady.",
                  email: "[email protected]", 
                  password: "foobar", 
                  password_confirmation: "foobar",
                  admin: true)

First of all, execute the following command.

% rails db:migrate:reset
% rails db:seed

Let's check if there is no problem with the above and the initial data is reflected. If there is no problem, add an image (avatar).

Describe the image in the seed file

First, add the images you want to use to app/assets/images. This time I added a file called cat.jpg.

Why did you define user earlier? That is to attach the image. Add avatar to the description of the seed file earlier.

db/seeds.rb


user = User.create(name: "Saya", 
                  profile: "It is an office lady.",
                  email: "[email protected]", 
                  password: "foobar", 
                  password_confirmation: "foobar",
                  admin: true)
user.avatar.attach(io: File.open(Rails.root.join('app/assets/images/cat.jpg')),
                  filename: 'cat.jpg')

And

% rails db:migrate:reset
% rails db:seed

Please run the. Let's check with the browser whether it is reflected!

Finally

There wasn't much information about ActiveStorage, so I hope it reaches the troubled beginners!

reference Attach file/IO object [Rails 5.2] How to use Active Storage Try new features of Rails6 64 (db: seed edition)

Recommended Posts

[Rails 6] Add images to seed files (using Active Storage)
[rails6.0.0] How to save images using Active Storage in wizard format
How to link images using FactoryBot Active Storage
[Rails] How to use Active Storage
[Rails API + Vue] Upload and display images using Active Storage
[Rails] How to upload images using Carrierwave
[Rails] Various ways to write in seed files
[Rails] Show avatars in posts using Active Storage
Add files to jar files
How to implement image posting function using Active Storage in Ruby on Rails
[rails] How to post images
[Rails] Save images using carrierwave
[Rails] Add column to devise
How to write Rails seed
[Rails] How to make seed
Add an icon to the header link using Rails fontawesome
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] How to add new pages
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
Upload multiple images to Cloudinary on Active Storage and publish to Heroku
[Rails] Add strong parameters to devise
[Rails] How to upload images to AWS S3 using refile and refile-s3
[Rails] Reflection to db using seeds.rb
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
Migration file to add comment to Rails table
* Android * Saving / loading files to internal storage
[Active Storage] Validation settings when uploading files
[Rails] [Memo] When to add = to <%%> and when not
How to implement image posting using rails
Post / delete multiple images with Active Storage
[Rails] How to handle data using enum
[Rails] Add page nation to table [For beginners]
POST images from Android to PHP using Retrofit
[Rails] How to create a graph using lazy_high_charts
How to add / remove Ruby on Rails columns
How to conditionally add html.erb class in Rails
Rails "How to delete NO FILE migration files"
Create a drag-and-drop markdown editor in Rails 6 (using Active Storage, SimpleMDE and Inline Attachment)
Implement a reservation system using Rails and simple calendar! Let's add validation to datetime!
* Android * Saving / loading files to internal storage
POST images from Android to PHP using Retrofit
How to link images using FactoryBot Active Storage
[Android] Uploading images from your device to the server
[Rails 6] Add images to seed files (using Active Storage)
Android app to select and display images from the gallery
Zip and upload multiple files to Firebase Storage on Android.