Sometimes executes code before the needed JS file is loaded

Community Driven Extensions Project
Post Reply
Parisian
Posts: 1
Joined: 21 May 2021, 07:04

21 May 2021, 07:06

I have two working view pages (edit and new) that include a text area to which I'm trying to add CKEditor. The view page includes:

Code: Select all

<%= f.text_area :page %>                 // This produces a textarea with id image_page

<script>
  CKEDITOR.replace( 'image_page' );      // I also tried wrapping this in `$(id='image_page').ready(function () {` `CKEDITOR.replace( 'image_page' );` `});` but that made no difference.
</script>
To the header of application.html.erb I added ckeditor.js:

Code: Select all

<% if controller_name == 'images' && ['new', 'edit'].include?(action_name) %>
  <script src="/assets/ckeditor.js"></script>
<% end %>
The custom base path (all files are in public/assets/ckeditor453/) is added to application.js. I did not require ckeditor.js here since that's already included in the above header:

Code: Select all

var CKEDITOR_BASEPATH = '/assets/ckeditor453/';
When visiting the new or edit view via a link in another view (very standard links: e.g. edit_image_path(image)), then the new or edit page loads without the ckeditor and there's a JS error saying ReferenceError: CKEDITOR is not defined. Upon refreshing the same page, it loads correctly with ckeditor present (and the error is thus gone).
When visiting the new or edit path directly (thus not via a link but entering their address in the browser's location bar), the page immediately loads correctly with the need for reloading i



kabosu coin

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 10 guests