Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Civil21 opened this issue Jul 7, 2022 · 9 comments
Closed

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

Civil21 opened this issue Jul 7, 2022 · 9 comments

Comments

@Civil21
Copy link

Civil21 commented Jul 7, 2022

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 Civil21 changed the title Action Text. Do not display content in editor. (Action Text) Does't display content in the editor Jul 7, 2022
@EricRoos
Copy link

Thank you for this!

@acollazomayer
Copy link

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

@CamilaUlmetePais
Copy link

@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?

@BBVishalkumar
Copy link

Thank you for this!

Can you explain how this worked @EricRoos

@blocknotes
Copy link
Owner

blocknotes commented Oct 5, 2023

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
Copy link
Owner

blocknotes commented Feb 27, 2024

Here it is another alternative version using getter/setter methods (in case you prefer to avoid having a decorator object).

A sample model:

# app/models/author.rb
class Author < ApplicationRecord
  has_rich_text :description

  def quill_description
    self.description&.body&.html_safe
  end

  def quill_description=(value)
    self.description = value
  end
end

The admin model configuration:

# app/admin/authors.rb
ActiveAdmin.register Author do
  permit_params :name, :email, :age, :quill_description

  show do
    attributes_table do
      row :name
      row :email
      row :age
      row :quill_description
    end
    active_admin_comments
  end

  form do |f|
    f.inputs 'Author' do
      f.input :name
      f.input :email
      f.input :age
      f.input :quill_description, as: :quill_editor
    end

    f.actions
  end
end

@BBVishalkumar
Copy link

Hi @blocknotes
This is able to show the text but not able to show the color
suppose i have made my text in red color now when i update it and again edit it it not showing the red color why?

@BBVishalkumar
Copy link

BBVishalkumar commented Mar 6, 2024

Hi @blocknotes
For showing the color and background color and other text coloring stuff
we need to update the method
def quill_description self.description&.body&.html_safe end

@blocknotes
Copy link
Owner

Hi @blocknotes For showing the color and background color and other text coloring stuff we need to update the method def quill_description self.description&.body&.html_safe end

Thank you @BBVishalkumar - I update my last answer 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants