activeadmin_quill_editor icon indicating copy to clipboard operation
activeadmin_quill_editor copied to clipboard

(Action Text) Does't display content in the editor

Open Civil21 opened this issue 2 years ago • 1 comments

Dependencies:

- Rails (7.0.3)
- Ruby (3.1)
- activeadmin_quill_editor (1.0.0)

Situation: If used with Action Text there is a problem. When editing, the editor is empty because it is not possible to read the input content due to <div class="trix-content">some html content</div>

Of course, this is not a problem of the gem, but since it is recommended to use it when you need to work with Action Text in the active admin, it is worth offering a solution.

My solution (hardcode):

project/app/models/some_model.rb

has_rich_text :text

def some_text=(body)
 text.body = body
end

def some_text
   text.to_s.gsub(/\A<div class="trix-content">(.*)<\/div>\z/m, '\1').strip.html_safe
end

Civil21 avatar Jul 07 '22 17:07 Civil21

Thank you for this!

EricRoos avatar Jul 26 '22 16:07 EricRoos

@Civil21 can you please explain the solution a little bit better. I am not being able to make it work. Thanks!

acollazomayer avatar Apr 18 '23 09:04 acollazomayer

@Civil21 can you please explain the solution a little bit better. I am not being able to make it work. Thanks!

I second this! Please @Civil21 , if at all possible, could you explain how this works?

CamilaUlmetePais avatar Jul 19 '23 14:07 CamilaUlmetePais

Thank you for this!

Can you explain how this worked @EricRoos

BBVishalkumar avatar Sep 28 '23 06:09 BBVishalkumar

Hey @Civil21 and the other users: I'm sorry to comment so late on this issue 😓

Situation: If used with Action Text there is a problem. When editing, the editor is empty because it is not possible to read the input content due to <div class="trix-content">some html content</div> Of course, this is not a problem of the gem, but since it is recommended to use it when you need to work with Action Text in the active admin, it is worth offering a solution.

activeadmin_quill_editor supports only plain text or string fields. ActionText (or any other field wrapper) could interfere with the editor functions, as in this case. So I would suggest to avoid mixing components in this way.

That said, I provide a different solution - because from my standpoint - overriding the model's methods is not the best, you could break ActionText functions then.

I would use a decorator instead: https://activeadmin.info/11-decorators.html (with draper gem - or you could use any other delegation mechanism)

My example:

# app/models/book.rb
class Book < ApplicationRecord
  has_rich_text :content
end

# app/admin/books.rb
class BookDecorator < Draper::Decorator
  delegate_all

  def content
    super.to_s
  end
end

ActiveAdmin.register Book do
  decorate_with BookDecorator

  permit_params :content

  form decorate: true do |f|
    f.input :content, as: :quill_editor
  end

  show do
    attributes_table do
      row :content do |book|
        book.content.to_s
      end
    end
  end
end

I'm closing the issue.

blocknotes avatar Oct 05 '23 08:10 blocknotes