Rails6: Extract the image in Action Text

Premise

Rails:6.0.3.2 Model name to use: Article Rich text field name: body

Actually take out the image


@article = Article.first
@images = @article.body.embeds
ActionText::RichText Load (0.5ms)  SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 1 AND `action_text_rich_texts`.`record_type` = 'Article' AND `action_text_rich_texts`.`name` = 'body' LIMIT 1

When you want to retrieve the first image

@images.first.blob
ActiveStorage::Attachment Load (0.6ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'ActionText::RichText' AND `active_storage_attachments`.`name` = 'embeds' ORDER BY `active_storage_attachments`.`id` ASC LIMIT 1
ActiveStorage::Blob Load (0.4ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 1 LIMIT 1
=> #<ActiveStorage::Blob id: 1, key: "rt0856j7x8btuev1ncbuurjgjyq4", filename: "1_xxo_q17kxed6bI9O_E8ZcA.png ", content_type: "image/png", metadata: {"identified"=>true, "width"=>512, "height"=>512, "analyzed"=>true}, byte_size: 14483, checksum: "5qx1/QPniLbLvo07uUFsOw==", created_at: "2020-07-16 11:40:19">

check existence

@images.present?
ActiveStorage::Attachment Exists? (0.6ms)  SELECT 1 AS one FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'ActionText::RichText' AND `active_storage_attachments`.`name` = 'embeds' LIMIT 1
=> true

Recommended Posts

Rails6: Extract the image in Action Text
Rails Action Text implementation
[Rails] How to display an image in the view
JavaFX-Load Image in the background
About the symbol <%%> in Rails erb
Replace preview by uploading by clicking the image in file_field of Rails
The identity of params [: id] in rails
Rails refactoring story learned in the field
[Rails] Reset the database in the production environment
Display the background image in the production environment
[Rails] About the error that the image is not displayed in the production environment
Change the save destination of the image to S3 in the Rails app. Part 2
[Order method] Set the order of data in Rails
Try adding text to an image in Scala using the Java standard library
I tried to organize the session in Rails
[Rails] Show multi-level categories in the breadcrumb trail
Group_by in Rails
Implement the Like feature in Ajax with Rails.
[Rails 6 + Action Mailbox] Those who extracted the plain text of Gmail received by Action Mailbox
Put the image obtained from http in NotificationCompat
How to check Rails commands in the terminal
[Ruby / Rails] Set a unique (unique) value in the class
[Ruby on Rails] Post image preview function in refile
Read the Rails Guide (Overview of Action Controller) again
Questions about implementing the Like feature (Ajax) in Rails
Model association in Rails
Adding columns in Rails
Disable turbolinks in Rails
CSRF measures in Rails
Use images in Rails
Split routes.rb in Rails6
Implement markdown in Rails
The story when the test folder was not created in Rails
Launch the Rails app locally in production mode (API Server)
[Ruby on Rails] Quickly display the page title in the browser
[Rails] Where to be careful in the description of validation
Use your own classes in the lib directory with Rails6
[Rails] How to display information stored in the database in view