rails下使用rich editor kindeditor和ckeditor对比记录
本文原来是介绍ckeditor的,但后来ckeditor被我弃用。原因是我找到了更好的richeditor,那就是kindeditor。
kindeditor配置安装都简单,而且更轻便,可以和carrywave连用进行上传,也有rails的一键安装程序,自己去github上面找。而ckeditor界面太复杂了,而且和rails连用时的那个github上的程序是用paperclip进行上传的,paperclip我居然没有成功使用起来,:-)
ckeditorhttps://github.com/galetahub/ckeditor我觉得功能挺全,支持ajax上传图片等功能,支持中文,界面可以定制,和rails集成了,就用了一用。
按照github的文档安装,期间在执行railsgenerateckeditor:install的时候报错:
gsubpublic/javascripts/ckeditor/plugins/image/dialogs/image.js
fetchingrails.js
/home/zj/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:678:in`connect':SSL_connectreturned=1errno=0state=SSLv3readservercertificateB:certificateverifyfailed(OpenSSL::SSL::SSLError)
from/home/zj/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:678:in`blockinconnect'
from/home/zj/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/timeout.rb:44:in`timeout'
from/home/zj/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/timeout.rb:89:in`timeout'
from/home/zj/.rvm/rubies/ruby-1.9.2-
...
from/home/zj/.rvm/gems/[email protected]/gems/ckeditor-3.6.3/lib/generators/ckeditor/install_generator.rb:59:in`download_javascripts'
...
fromscript/rails:6:in`require'
fromscript/rails:6:in`<main>'
解决办法:修改install_generator.rb
在classInstallGenerator<Rails::Generators::Base这一行后面加:
require'openssl'
OpenSSL::SSL::VERIFY_PEER=OpenSSL::SSL::VERIFY_NONE
就好了
我是按照
railsgenerateckeditor:install这样安装的,会被装到/public/javascripts/ckeditor下。也可按照
railsgenerateckeditor:install--path=public/assets这样的方法安装,因为这样会被装到/public/assets下,这个目录是railsassetspipline预编译的目标路径,也许更好吧。后来我把我安装文件拷贝到了/public/assets/javascripts/ckeditor下
后来报错
Sprockets::FileOutsidePathsinNews#new
Showing/home/zj/cms/app/views/news/new.html.hamlwhereline#2raised:
/javascripts/ckeditor/ckeditor.jsisn'tinpaths:/home/zj/cms/app/assets/images,/home/zj/cms/app/assets/javascripts,/home/zj/cms/app/assets/stylesheets,/home/zj/cms/vendor/assets/stylesheets,/home/zj/.rvm/gems/[email protected]/gems/jquery-rails-1.0.16/vendor/assets/javascripts
Extractedsource(aroundline#2):
%head
=javascript_include_tag"/javascripts/ckeditor/ckeditor.js"
错就错在最前面如果加/,导致查找/app下的目录路径,去掉/则查找public/assets目录下,这才是正确的
http://127.0.0.1:3000/assets/javascripts/ckeditor/_samples/index.html是例子
后来改config.js里面的language为zh-cn,但无效,因为生成的js里面有language:en。所以我自己重新封装了一下,方法如下:
application_helper.rb
module ApplicationHelper def ckeditor_js(name) raw "<script type=\"text/javascript\">\n<!-- /<![CDATA[ -->\nif (CKEDITOR.instances['"+name+"']) {CKEDITOR.remove(CKEDITOR.instances['"+name+"']);}CKEDITOR.replace('"+name+"', { language: 'zh-cn' });\n<!-- /]]> -->\n</script>" end end
_form.html.haml
=f.text_area :content =ckeditor_js "news_content"