tsechingho / ckeditor-rails

Integrate CKEditor javascript library with Rails asset pipeline
https://github.com/tsechingho/ckeditor-rails
MIT License
213 stars 132 forks source link

ckeditor not displaying until page reload #28

Open hello-gawjus opened 11 years ago

hello-gawjus commented 11 years ago

Hi tsechingho, I'm using rails 4, bootstrap and ckeditor_rails.

Using the following in my _form view:

<%= f.text_area :content, :class => 'ckeditor' %>

results in a regular bootstrap flavored text_area the first time I visit new or edit. However, if I reload the page, it works. The text_area becomes fully ckeditor enabled.

I have tried loading js file (//= require ckeditor-jquery) before and after bootstrap but this has no effect.

Any ideas?

hello-gawjus commented 11 years ago

Actually this may have something to do with turbolinks. When I hit the new url from the browser address bar ckeditor works. When I hit it from a menu item it doesn't. So it's not really about page reloads. It's seems to be about turbolinks compatibility.

Any suggestions would still be appreciated.

hello-gawjus commented 11 years ago

Removing //= require turbolinks from application.js does resolve the issue. Is there a turbolinks compatibility solution?

tsechingho commented 11 years ago

you can disable turbolinks by specific tag attribute.

<div data-no-turbolink>
<%= f.text_area :content, :class => 'ckeditor' %>
</div>
yosepkim commented 10 years ago

I am having the same issue.

xescugc commented 10 years ago

At the documentation of Turbolinks you can found a 'solution' https://github.com/rails/turbolinks/#opting-out-of-turbolinks , it works for me if added it to the link that brings me to the ckeditor page.

Ryel commented 10 years ago

Thanks for raising this issue - saved me a lot of time

Kyle-Falconer commented 10 years ago

None of these were working for me. I ended up punting and just removing turbolinks completely from my project. That fixed it for me.

cweilemann commented 10 years ago

+1 with @netinept - I tried adding data-no-turbolink to the parent div of a textarea, and still had no luck. The only solution I've found so far is to remove turbolinks completely.

mwanas commented 10 years ago

I have tried to include $('.ckeditor').ckeditor(); on each page that uses checkeditor and now it is working fine

trkrameshkumar commented 9 years ago

This works for me without removing turbo links. Add a id for ckeditor textara Add this script on every bottom of the page where ever you want to enable the ckeditor,

<script>
        CKEDITOR.replace('element_id')
</script>

Replace the element_id with your element id

Note: element_id should not contain id selector($ or .)

AllaKishore commented 9 years ago

ckeditor not displaying until page reload in angularjs give me solution

WiechersV commented 9 years ago

Still an issue

isratmir commented 9 years ago
  $(document).on('page:update', function(){
      console.log('page updated');
      if ($('.ckeditor')[0]) {
          CKEDITOR.replace($('.ckeditor').attr('id'));
      }
  });
ghost commented 9 years ago

could you be more explicit please ? i use simple_form with bootstrap, my views _form constructed with haml (rails-4.2.2) nothing works with turbolinks and ckeditor, but also where to add the jquery script ? i can not read it in your answer @isratmir . also, if i comment turbolinks from views/layout/application.haml, no error, but not more ckeditor. Lack of knowledge and informations around this ckeditor and this turbolinks "data-no-turbolink" with simple_form... thanks for provide little bit more information guys (if possible).

isratmir commented 9 years ago

@jerome2 try to add to application.js in app/assets/javascript folder

ghost commented 9 years ago

waiting guys, i think is not the good area for post comment issue. first i installed gem jquery-turbolinks (and restart nginx server) then no more problem with turbolinks. (need your jquery good code for update the page alone and see it directly) second, if i not use the way of simple_form in my form, it works, so this is to ask at simple_form gem issues area. but thanks again for help.

kelso commented 9 years ago

You can keep turbolinks enabled, just reinitialize ckeditor each time page is changed (turbolinks event):

Create custom app/javascripts/ckeditor/reinit.js:

$(document).bind('page:change', function() {
  $('.ckeditor').each(function() {
    CKEDITOR.replace($(this).attr('id'));
  });
});

And add class='ckeditor' to your <textarea>. That's all.

ghost commented 9 years ago

@kelso: thanks, this works also, but for some ther stuff, jquery-turbolinks help me. that's fine, thank you.

cbstodd commented 8 years ago

Thanks @kelso, that did the trick.

u007 commented 8 years ago

for those using turbolinks5

jQuery(document).on('turbolinks:load', (e)->
    #do your initialize here
    $('textarea.ckeditor').each(->
        if $(this).css('visibility') != 'hidden'
            CKEDITOR.replace(this)
    )
    if Turbolinks
        Turbolinks.Cache()
)
nwshane commented 8 years ago

@kelso's method works for Turbolinks Application Visits but not for Restoration Visits (when the user uses the forward and back buttons).

https://github.com/turbolinks/turbolinks#navigating-with-turbolinks

This is the code I'm using:

var RichTextArea = (function() {
  var exports = {};
  var selector = '.js-become-rich-text-editor';

  function removeInstance(editor_id) {
    // Remove any old ckeditor elements from page
    $('.cke_editor_' + editor_id).remove();

    // Remove CKEditor instances from JavaScript
    CKEDITOR.remove(CKEDITOR.instances[editor_id]);
  }

  exports.load = function() {

    // Get already loaded instances
    var loaded_instances = Object.getOwnPropertyNames(CKEDITOR.instances);

    $(selector).each(
      function() {
        var editor_id = $(this).attr('id');

        // Remove CKEditor instance if already loaded.
        // Necessary for compatibility with Turbolinks restoration visits,
        // in which the instances are not removed
        if (loaded_instances.includes(editor_id)) {
          removeInstance(editor_id);
        }

        // Initialize CKEditor instance
        CKEDITOR.replace($(this).attr('id'));
      }
    );

  }

  return exports;
})();

$(document).on('page:change', function() {
  RichTextArea.load();
});

To use, just add the class js-become-rich-text-editor to any editors you want to change into CKEditors.

Explanation:

karlingen commented 8 years ago

You can solve this by using data: { no_turbolink: true } on the link_to helper.

The a element linking to the page where the ckeditor textarea is located must have data-no-turbolink="true" set in order to instruct turbolinks to be temporarily disabled for that page.

guyviner commented 7 years ago

trkrameshkumar 's solution commented on 22 Sep 2014 worked for me if you're using AJAX, place this script at the bottom of your new.js.erb file CKEDITOR.replace('element_id'); Don't forget to add an id of 'element_id' to your text_area tag in the view file, like so: f.cktext_area :foobar, id:"element_id" . finally a solution thank you!

zw963 commented 6 years ago

@u007 's solution worked for me, rails version: 5.1.4

But Turbolinks.Cache()' is failed,TypeError: Turbolinks.Cache is not a function`.

u007 commented 6 years ago

@zw963 remove the cache part

zw963 commented 6 years ago

@u007, thanks, that worked.