spohlenz / tinymce-rails

Integration of TinyMCE with the Rails asset pipeline
Other
813 stars 256 forks source link

Can't init editor manually #305

Closed dmitriyrotaenko closed 11 months ago

dmitriyrotaenko commented 11 months ago

I'm new to rails and trying to create a simple app with tinymce as the page editor. I want to initialize the editor in a hotwired component but when I run bin/dev, I get this error in the terminal:

22:15:46 js.1   | ✘ [ERROR] Could not resolve "tinymce"
22:15:46 js.1   |
22:15:46 js.1   |     app/javascript/controllers/editor_controller.js:2:7:
22:15:46 js.1   |       2 │ import "tinymce"

Gemfile

ruby "3.1.2"
gem "rails", "~> 7.0.5", ">= 7.0.5.1"
gem "tinymce-rails", "~> 6.6"

/app/javascript/application.js

import "@hotwired/turbo-rails"
import "./controllers"
//= require tinymce

/app/javascript/controllers/editor_controller.js

import { Controller } from "@hotwired/stimulus";
import "tinymce"

export default class extends Controller {

  connect() {
    tinymce.init({ selector: "textarea.editor" })
  }
}

My form partial

= form_with(model: [@project, @page], class: "contents", data: { controller: "editor" }) do |form|
  - if page.errors.any?
    #error_explanation.bg-red-50.text-red-500.px-3.py-2.font-medium.rounded-lg.mt-3
      %h2
        = pluralize(page.errors.count, "error")
        prohibited this page from being saved:
      %ul
        - page.errors.each do |error|
          %li= error.full_message
  .my-5
    = text_area_tag :editor, "", :rows => 40, :cols => 120

What am I missing?

dmitriyrotaenko commented 11 months ago

Was missing the following code in application.html.haml

    = tinymce_assets
    %script{src: "/assets/tinymce.js", type: "text/javascript"}