#28 WYSIWYG Editor with Trix


Compose beautifully formatted text in your web application. Trix is a WYSIWYG editor for writing messages, comments, articles, and lists.
rails wysiwyg view ajax javascript 4:16


Gemfilegem 'trix'
application.js//= require trix
application.css.scss *= require trix
_form.html.erb<%= f.trix_editor :summary %>
articles.coffee$ ->
  document.addEventListener 'trix-attachment-add', (event) ->
    attachment = event.attachment
    if attachment.file
      return sendFile(attachment)

  document.addEventListener 'trix-attachment-remove', (event) ->
    attachment = event.attachment
    deleteFile attachment

  sendFile = (attachment) ->
    file = attachment.file
    form = new FormData
    form.append 'Content-Type', file.type
    form.append 'image[image]', file
    xhr = new XMLHttpRequest
    xhr.open 'POST', '/images', true

    xhr.upload.onprogress = (event) ->
      progress = undefined
      progress = event.loaded / event.total * 100
      attachment.setUploadProgress progress

    xhr.onload = ->
      response = JSON.parse(@responseText)
        url: response.url
        image_id: response.image_id
        href: response.url

    xhr.send form

  deleteFile = (n) ->
      type: 'DELETE'
      url: '/images/' + n.attachment.attributes.values.image_id
      cache: false
      contentType: false
      processData: false


no name said about 3 years ago on WYSIWYG Editor with Trix :

Hey, nice screencast!

It was really useful, but I've noticed one thing. There is no CSRF protection for file uploads and deletes. This could be really dangerous and I think that it should be fixed.

Btw. commenting is painful. I had to sign up, confirm my email, etc. I had to take a lot of different steps in order to write this comment. It would be great if you could simplify this process.

Pablo Bello said over 2 years ago on WYSIWYG Editor with Trix :

To add CSRF:

xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
xhr.setRequestHeader 'X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')

chunkster29 said over 2 years ago on WYSIWYG Editor with Trix :

Could  it be integrated with the aws-s3 gem

Login to Comment