1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Ruby on Rails
Find a file
George Claghorn 1a4a582ddc Install Active Storage extension lazily
Avoid eagerly loading Active Record during app boot. Reinstall the extension when ActiveStorage::Blob is reloaded in dev.
2018-10-03 22:26:15 -04:00
app Declare prior to use 2018-10-03 17:02:54 -07:00
bin Pull activetext.js into dummy app with webpacker 2018-02-12 10:34:29 -05:00
db/migrate Never mind on allowing blank 2018-09-12 15:51:06 -07:00
lib Install Active Storage extension lazily 2018-10-03 22:26:15 -04:00
test Fix content attachment test 2018-10-03 15:06:49 -04:00
.gitattributes Initial commit 2018-02-07 12:43:36 -06:00
.gitignore Pull activetext.js into dummy app with webpacker 2018-02-12 10:34:29 -05:00
.ruby-version Tests: Add Message scaffolding 2018-02-08 16:02:16 -05:00
actiontext.gemspec The world is our stage 2018-10-03 16:07:08 -07:00
Gemfile Add dummy app 2018-02-08 11:01:46 -06:00
Gemfile.lock Use released version of Rails 5.2.0 as the dependency 2018-10-01 17:04:46 -07:00
LICENSE Initial commit 2018-02-07 12:43:36 -06:00
package.json The world is our stage 2018-10-03 16:07:08 -07:00
Rakefile Rename from Active Text to Action Text 2018-04-13 16:23:04 -07:00
README.md Flush out the README 2018-10-02 17:10:09 -07:00
yarn.lock Add initial ActiveStorage integration 2018-02-12 15:50:42 -05:00

Action Text

Action Text brings rich text content and editing to Rails. It includes the Trix editor that handles everything from formatting to links to quotes to lists to embedded images and galleries. The rich text content generated by the Trix editor is saved in its own RichText model that's associated with any existing Active Record model in the application. Any embedded images (or other attachments) are automatically stored using Active Storage and associated with the included RichText model.

Trix compared to other rich text editors

Most WYSIWYG editors are wrappers around HTMLs contenteditable and execCommand APIs, designed by Microsoft to support live editing of web pages in Internet Explorer 5.5, and eventually reverse-engineered and copied by other browsers.

Because these APIs were never fully specified or documented, and because WYSIWYG HTML editors are enormous in scope, each browsers implementation has its own set of bugs and quirks, and JavaScript developers are left to resolve the inconsistencies.

Trix sidesteps these inconsistencies by treating contenteditable as an I/O device: when input makes its way to the editor, Trix converts that input into an editing operation on its internal document model, then re-renders that document back into the editor. This gives Trix complete control over what happens after every keystroke, and avoids the need to use execCommand at all.

Installing

Assumes a Rails 6+ application with Active Storage and Webpacker installed.

  1. Install the gem:

    # Gemfile
    gem "actiontext", github: "rails/actiontext", require: "action_text"
    gem "image_processing", "~> 1.2" # for Active Storage variants
    
  2. Install assets, npm dependency, and migrations

    ./bin/rails action_text:install
    ./bin/rails db:migrate
    

Examples

Adding a rich text field to an existing model:

# app/models/message.rb
class Message < ActiveRecord::Base
  has_rich_text :content
end

Then refer to this field in the form for the model:

<%# app/views/messages/_form.html.erb %>
<%= form_with(model: message) do |form| %>
  …
  <div class="field">
    <%= form.label :content %>
    <%= form.rich_text_area :content %>
  </div>
  …
<% end %>

And finally display the rich text on a page:

<%= @message.content %>

To accept the rich text content, all you have to do is permit the referenced attribute:

class MessagesController < ApplicationController
  def create
    message = Message.create! params.require(:message).permit(:title, :content)
    redirect_to message
  end
end

Custom styling

By default, the Action Text editor and content is styled by the Trix defaults. If you want to change these defaults, you'll want to remove the app/assets/stylesheets/actiontext.css linker and base your stylings on the contents of that file.

You can also style the HTML used for embedded images and other attachments (known as blobs). On installation, Action Text will copy over a partial to app/views/active_storage/blobs/_blob.html.erb, which you can specialize.

Development road map

Action Text is destined for inclusion in Rails 6, which is due to be released some time in 2019. We will refine the framework in this separate rails/actiontext repository until we're ready to promote it via a pull request to rails/rails.

License

Action Text is released under the MIT License.