ruby-on-railsalignmenttrixactiontext

Adding alignment buttons to Trix Editor (ActionText Ruby on Rails)


I followed this tutorial to add alignment options to Trix text editor and the editor in my app works similar to the repository referenced by the tutorial. Meaning, clicking the align right button, will align text to the right, but when I save the form by clicking Submit button (my rich_text field is a part of the simple_form form), the text that I had previously aligned to the right, is now saved on the left (in its default position). This is the text while being edited (before submit):

<align-right><strong>Hello</strong></align-right>

and this is the text after being saved:

<strong>Hello</strong>

Added

ActionText::ContentHelper.allowed_attributes += %w[ align-right align-center align-left ]

in config/initializers/action_text.rb but it didn't make any difference...


Solution

  • It seems that your text is being sanitized by ActionText. Try

    config.after_initialize do
      ActionText::ContentHelper.allowed_tags.add 'align-right'
      ActionText::ContentHelper.allowed_tags.add 'align-center'
      ActionText::ContentHelper.allowed_tags.add 'align-left'
    end
    

    in your application.rb file.