开发者

Is it possible to use tinyMCE with rails_admin?

开发者 https://www.devze.com 2023-03-22 00:07 出处:网络
I really like rails_admin, but my clients don\'t like CKEditor. Is this really the only option for WYSIWYG on this gem? Is there any way 开发者_运维知识库to use tinyMCE with rails_admin?after struggli

I really like rails_admin, but my clients don't like CKEditor. Is this really the only option for WYSIWYG on this gem? Is there any way 开发者_运维知识库to use tinyMCE with rails_admin?


after struggling to get CKEditor working properly in RailsAdmin (on Rails 3.1), I used tinymce: It works well and is done in minutes:

in your gemfile add:

gem 'tinymce-rails'

plus you inlcude a line in rails_admin.js.erb:

require_asset 'tinymce-jquery'

you may need to copy the whole file (rails_admin.js.erb) from the gem to /assets/javascripts/rails_admin/ before you do this.

finally, you will also need to add some jquery to the view files
app/views/rails_admin/main/edit.html.haml and app/views/rails_admin/main/new.html.haml

 :javascript 
   jQuery(function() { 
     jQuery('textarea').tinymce({ 
       theme: 'advanced' 
     }); 
   }); 

This will add the Wysiwyg to all text area fields.


WYSIWYG editors typically just overlay an HTML text area element with JavaScript functionality. So any editor should work in theory. You could replace the references in the code to tinyMCE, make sure you have all the files properly installed and then set tinyMCE to use the ID of the text area control.

It should not make a difference to the back-end programming which client side interface is used to create HTML in the text area.


Great andistuder, I propose a modified version of your solution.

Copy the rails_admin.js.erb from the gem to the /assets/javascripts/rails_admin/ in your project path. Add the following line:

...
require_asset 'tinymce-jquery'
%>
jQuery(function() {
     jQuery('textarea').tinymce({
       theme: 'advanced'
     });
});

And all will works like a charm!


I achieved this in a slightly different way, which worked for:

  • rails_admin (0.8.1)
  • tinymce-rails (4.4.1)

Implementation:

Gemfile

gem 'tinymce-rails'

app/assets/javascripts/rails_admin/custom/ui.js

//= require tinymce-jquery

var admin = {
  initTinyMce: function() { tinyMCE.init({ selector: "textarea[name$='_html]'" }) }
}

$(function() {
  admin.initTinyMce();

  $(document).on('rails_admin.dom_ready', function() {
    admin.initTinyMce();
  });
});

Explanation

https://github.com/sferik/rails_admin/wiki/Theming-and-customization

  • Suggests the ui.js location
  • Suggests to use rails_admin.dom_ready

https://github.com/spohlenz/tinymce-rails

  • //= require tinymce-jquery: no ruby interpolation required and I like using manifest files

Custom:

  • I needed to run the initialization twice, all seems good so far
  • The selector textarea[name$='_html]' will convert all text areas where the field ends in _html, that's what I store, that's how I name my fields
0

精彩评论

暂无评论...
验证码 换一张
取 消