Skip to content

Instantly share code, notes, and snippets.

@blocknotes
Last active February 24, 2025 15:40
Show Gist options
  • Select an option

  • Save blocknotes/74baceccc74c12c60a60a42d112787a3 to your computer and use it in GitHub Desktop.

Select an option

Save blocknotes/74baceccc74c12c60a60a42d112787a3 to your computer and use it in GitHub Desktop.
ActiveAdmin Trix editor setup (tested with Rails 6.0.3.3)
  • Setup the Rails project with ActiveAdmin using Webpacker: rails g active_admin:install --use_webpacker
  • Setup Trix editor for ActiveAdmin:
    • Execute: bin/rails action_text:install
    • Add Javascript library to app/javascript/packs/active_admin.js:
require("trix")
require("@rails/actiontext")
  • Add style library to app/javascript/stylesheets/active_admin.scss:
@import "trix/dist/trix";
  • Add the rich field to the model (ex. Post): has_rich_text :content
  • Add the editor to the admin form:
  form do |f|
    f.inputs 'Post' do
      f.input :title
      li do
        label :content, class: 'trix-editor-label'
        f.rich_text_area :content
      end
    end
    # ...
  end
  • To output the content in the show, add to the admin show block:
  show do
    attributes_table do
      row :title
      row :content do
        div resource.content
      end
    end
  end
  • Minor optional style improvements to app/javascript/stylesheets/active_admin.scss:
body.active_admin {
  trix-editor {
    background: #fff;
    max-height: 300px;
    overflow-y: scroll;
  }

  .trix-editor-label {
    float: none;
    margin-bottom: 10px;
  }
}
@tms-vunguyen
Copy link
Copy Markdown

Thank you so much.

@geraldoantonio
Copy link
Copy Markdown

perfect

@nayan24
Copy link
Copy Markdown

nayan24 commented Nov 14, 2022

still mine not working..help me with this i'm not able to call scss and js for trix....bold function is not working.

@kashifcodility
Copy link
Copy Markdown

Using Rails 7

go to active_admin.rb in intializer.then add

  config.register_stylesheet 'https://unpkg.com/trix@2.0.8/dist/trix.css'


 config.register_javascript "https://unpkg.com/trix@2.0.8/dist/trix.umd.min.js"

then in any active admin resource

 form do |f|
    f.inputs 'Post' do
    f.input :title
    li do
       label :content, class: 'trix-editor-label'
       f.rich_text_area :content
    end
   end
# ...
  end

@arish-me
Copy link
Copy Markdown

@kashifcodility thanks its working for me.

@xliu8
Copy link
Copy Markdown

xliu8 commented Nov 8, 2024

@kashifcodility Thank you!

@Sprachprofi
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment