Rails6: Input the initial data of ActionText using seed

Introduction

I tried using ActionText, which became available from Rails6, but I stumbled when I put in the initial data, so I decided to write about that time.

Premise

Rails:6.0.3.2 Model name to use: Post Column: title Rich text field name: content

Input of seed

First, make some posts easily

15.times do
  Post.create!(title: Faker::Book.unique.title)
end

After that, you can put the initial data of ActionText by putting the data associated with Post into the ʻaction_text_rich_texts` table.

I think it will actually look like this.

Post.all.each do |post|
  ActionText::RichText.create!(record_type: 'Post', record_id: post.id, name: 'content', body: Faker::Lorem.sentence)
end

Set record_type to the model name that uses ActionText, recor_id to the id of post, name to the rich text field name, and body to the text you actually want to put.

Finally, put it in and it's done.

rails db:seed

If you can display it on the view side with this, I think that the input data is displayed.

Bonus: Set line breaks and bold characters as if posted from ActionText

If you want to insert line breaks and bold characters in the initial data, set as follows.

content = '<div class="trix-content">
  <div><strong>Here is bold</strong>is.<br><br>The text here is broken</div>
</div>'
rich = ActionText::RichText.last
rich.update(body: content)

If you check the view side with this, you should be able to prepare the initial data with line breaks and bold characters.

Digression

I wonder if articles with images can be prepared from the initial data. ..

Recommended Posts

Rails6: Input the initial data of ActionText using seed
Initial data input with [Rails] seed_fu!
[Enum] Let's improve the readability of data by using rails enum
[Ruby on Rails] Introduction of initial data
What is the data structure of ActionText?
[Order method] Set the order of data in Rails
[Rails] Display of multi-layered data using select boxes
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
Create an EC site with Rails5 ⑥ ~ seed data input ~
[Rails] Implementation of multi-layer category function using ancestry "seed"
Try using the query attribute of Ruby on Rails
[Rails] Register by attribute of the same model using Devise
A note about the seed function of Ruby on Rails
[Rails] Check the contents of the object
Explanation of the order of rails routes
Filter the fluctuations of raw data
[Docker] Input initial data to docker-entrypoint-initdb.d without mounting the directory [MySQL]
[Rails 6] destroy using the resources method
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Rails] I will explain the implementation procedure of the follow function using form_with.
I measured the performance of the in-memory data grid Apache Ignite using Yardstick.
[Ruby On Rails] How to search the contents of params using include?
Try using || instead of the ternary operator
[Rails] Temporary retention of data by session
Test the integrity of the aggregation using ArchUnit ②
The identity of params [: id] in rails
Try using the Rails API (zip code)
part of the syntax of ruby ​​on rails
[Rails] Manage data for each seed file
[Rails] Get standard input for multi-line data
[Rails] Change the label name of f.label
Acquisition of input contents using Scanner (Java)
The process of introducing Vuetify to Rails
The contents of the data saved by CarrierWave.
[Rails] How to handle data using enum
Test the integrity of the aggregation using ArchUnit ③
Pay attention to the boundary check of the input value when using the float type
How to make a unique combination of data in the rails intermediate table
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
Back calculation of the transition of the internal seed of Random
[Rails] Button to return to the top of the page
Add empty data to the top of the list
[Ruby on Rails] Until the introduction of RSpec
[Rails] Implementation of image enlargement function using lightbox2
I understood the very basics of character input
[Rails] Implementation of batch processing using whenever (gem)
How to display the result of form input
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Note] Summary of rails login function using devise ①
I tried using the profiler of IntelliJ IDEA
[Rails] Sort the post list by date or number of likes using the pull-down box
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap