Amit Solanki

Avatar

A Ruby On Rails Programmer from India

How to toggle tinymce editor in rails

TinyMCE is an excellent WYSIWYG editor. The same can be used in rails with this plugin. In many cases, we need to toggle tinymce editor on/off. For this add the following lines to tiny_mce_helper.rb file of your plugin just before javascript_tag tinymce_js line in tiny_mce_init method.

tinymce_js += %Q! function toggleEditor(id) {
    if (\!tinyMCE.getInstanceById(id))
        tinyMCE.execCommand('mceAddControl', false, id);
    else
        tinyMCE.execCommand('mceRemoveControl', false, id);
    }
    !

And you can easily place a link beside your editor to toggle something similar to:

<%= link_to_function "Add/Remove Editor", "toggleEditor('name')" %>

Here name is the name of the textarea/textbox to which you are applying tinymce editor.

Recent releases of tinymce would require to use tinyMCE.get(id) instead of getInstanceById(id) in code above.

No Comments, Comment or Ping

Reply to “How to toggle tinymce editor in rails”

Before you go

Going so soon? May these links be a guide to web enlightenment.