1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actiontext
bogdanvlviv 67a9a86b1d
Test actiontext on Rails 6.0
- config.load_defaults 6.0 in the dummy app and
  fix the test since by default rails 6.0 configured
  does not generate "utf8" hidden input, see #32125
- Use `ActiveRecord::Migration[6.0]` in the dummy app
  since actiontext will be since Rails 6.0
- Fix `CreateActiveStorageTables` migration in the dummy app.
  Add `t.foreign_key :active_storage_blobs, column: :blob_id`
  It was added in 2ae3a29508.
- `rails/actiontext$ yarn install`
2019-01-05 15:24:27 +02:00
..
app
bin
db/migrate
lib
test Test actiontext on Rails 6.0 2019-01-05 15:24:27 +02:00
.gitignore
actiontext.gemspec
CHANGELOG.md
MIT-LICENSE
package.json
Rakefile
README.md
yarn.lock

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.

Installation

Run rails action_text:install to add the Yarn package and copy over the necessary migration.

Examples

Adding a rich text field to an existing model:

# app/models/message.rb
class Message < ApplicationRecord
  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 sanitized 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.

License

Action Text is released under the MIT License.