我正在使用Ruby on Rails构建一个简单的CMS,我决定使用WYSIWYG编辑器进行语法高亮,因此我选择了TinyMCE。虽然,在尝试使用它时,我遇到了几个问题:
第一个问题是我在尝试使用CMS运行测试博客时遇到错误。首先我包括
s.add_dependency "tinymce-rails"
进入mycms.gemspec
并运行
$ bundle install
然后我添加了
//= require tinymce
在我的CMS中排成application.js
。但当我将我的CMS插件(作为一个单独的宝石)包含到我的测试博客中时,由于错误导致我无法访问我的管理面板 - Rails找不到tinymce
而我需要包含{ {1}}也在我的测试应用程序的Gemfile中让它工作。 (与gem tinymce-rails
相同的问题 - 只有在将它包含到应用程序的Gemfile中之后才能使用它,尽管它存在于gem bootstrap-sass
第二个问题是编辑器本身没有初始化,也没有显示在我的页面中。因此,形式本身来了:
mycms.gemspec
而textarea刚刚失踪。我尝试了所有变种 - 包括
<%= form_for (@entry, as: :entry, url: entries_path) do |f| %>
<p>
<%= f.label :title %> <br/>
<%= f.text_field :title %>
</p>
<% @entry.class.content_attributes.each do |attr_name, attr_type| %>
<p>
<%= f.label :title %><br />
<%= f.text_area attr_name, as: attr_type, class:"tinymce", rows:"40", cols:"120" %>
</p>
<% end %>
<% end %>
<%= tinymce %>
或
//= require tinymce
进入测试应用的//= require tinymce-jquery
或CMS的application.js
,以及添加
application.js
直接在表单的开头,但这些变体都不起作用。我错过了什么或做错了什么?
我的CMS的完整代码可在Github
上找到答案 0 :(得分:1)
如果您尚未添加此javascript代码,请尝试添加以下内容:
$(document).ready(function() {
return tinymce.init({
selector: 'textarea',
height: 100,
width: 550,
menubar: false,
plugins: ['image', 'charmap', 'uploadimage'],
toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link uploadimage | charmap ',
theme: 'modern',
relative_urls: false,
remove_script_host: false,
document_base_url: (!window.location.origin ? window.location.protocol + '//' + window.location.host : window.location.origin) + '/',
setup: function(ed) {
ed.on('init', function() {
ed.getDoc().body.style.fontSize = '14px';
});
ed.on('change', function() {
ed.save();
});
}
});
});