如何在Rails 3.1(gem + Asset Pipeline)中配置CKEditor

时间:2012-02-04 16:17:44

标签: ruby-on-rails ckeditor asset-pipeline

我已经在我的Rails 3.1应用程序上从https://github.com/galetahub/ckeditor成功配置了ckeditor gem。我现在的问题是我无法弄清楚如何配置CKEditor。根据自述文件使用的文件在启用资产管道的Rails 3.1应用程序中根本不存在。

5 个答案:

答案 0 :(得分:22)

一旦我弄清楚抛出的错误信息,答案就很容易了。

<强> /应用/资产/ JavaScript的/ CKEditor的

CKEDITOR.editorConfig = function( config )
{
    config.toolbar_MyToolbar =
    [
        { name: 'document', items : [ 'NewPage','Preview' ] },
        { name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
        { name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','Scayt' ] },
        { name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'
                 ,'Iframe' ] },
                '/',
        { name: 'styles', items : [ 'Styles','Format' ] },
        { name: 'basicstyles', items : [ 'Bold','Italic','Strike','-','RemoveFormat' ] },
        { name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote' ] },
        { name: 'links', items : [ 'Link','Unlink','Anchor' ] },
        { name: 'tools', items : [ 'Maximize','-','About' ] }
    ];
}

这是重要的部分,将require_tree(包括ckeditor / config.js)放在 ckeditor的require之后: 的 /app/assets/javascript/application.js

//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_tree .

答案 1 :(得分:9)

所以我昨天通过省略CKEDITOR.editorConfig = function(config){}部分来完成Rails 4.0 rc1和Ruby 2.0的工作。

我在app / assets / javascripts / ckedtior / config.js中的最终代码是

  CKEDITOR.config.toolbar= [
    { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
  ]

答案 2 :(得分:4)

这是 Rails 4.1 ckeditor 4.1.0 更新答案,可自定义配置ckeditor工具栏。

在您看来,使用simple_form,您需要像下面这样配置输入:

_FORM.HTML.ERB

<%= simple_form_for(@foo) do |f| %>
  <%= f.input :bar, as: :ckeditor %>
  <%= f.button :submit %>
<% end %>

在您的Javascript资源中,您需要创建一个名为“ckeditor”的文件夹,并在其中创建一个名为“config.js”的文件

../的JavaScript / CKEDITOR / CONFIG.JS

CKEDITOR.editorConfig = function(config) {
  //config.language = 'es'; //this could be any language
  config.width = '725';
  config.height = '600';

  // Filebrowser routes
  // The location of an external file browser, that should be launched when "Browse Server" button is pressed.
  config.filebrowserBrowseUrl = "/ckeditor/attachment_files";
  // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Flash dialog.
  config.filebrowserFlashBrowseUrl = "/ckeditor/attachment_files";
  // The location of a script that handles file uploads in the Flash dialog.
  config.filebrowserFlashUploadUrl = "/ckeditor/attachment_files";
  // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Link tab of Image dialog.
  config.filebrowserImageBrowseLinkUrl = "/ckeditor/pictures";
  // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Image dialog.
  config.filebrowserImageBrowseUrl = "/ckeditor/pictures";
  // The location of a script that handles file uploads in the Image dialog.
  config.filebrowserImageUploadUrl = "/ckeditor/pictures";
  // The location of a script that handles file uploads.
  config.filebrowserUploadUrl = "/ckeditor/attachment_files";

// You could delete or reorder any of this elements as you wish
  config.toolbar_Menu = [
    { name: 'document', items: ['Source', '-', 'Save', 'NewPage', 'DocProps', 'Preview', 'Print', '-', 'Templates'] }, 
    { name: 'clipboard', items: ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] }, 
    { name: 'editing', items: ['Find', 'Replace', '-', 'SelectAll', '-', 'SpellChecker', 'Scayt'] }, 
    { name: 'tools', items: ['Maximize', 'ShowBlocks', '-', 'About'] }, '/', 
    { name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] }, 
    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl'] }, 
    { name: 'links', items: ['Link', 'Unlink', 'Anchor'] }, '/', 
    { name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] }, 
    { name: 'colors', items: ['TextColor', 'BGColor'] }, 
    { name: 'insert', items: ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'] }
  ];
  config.toolbar = 'Menu';
  return true;
};

application.js的配置是这样的,请注意ckeditor和require_tree的顺序重要

的application.js

//= require jquery
//= require jquery_ujs
//= require ckeditor/init
//= require_tree .

现在在你的ckeditor.rb中你应该取消注释这行“config.asset_path”并添加你之前创建的config.js文件的路径“/ assets / ckeditor /”

../ CONFIG /初始化/ CKEDITOR.RB

# Use this hook to configure ckeditor
Ckeditor.setup do |config|
  # ==> ORM configuration
  # Load and configure the ORM. Supports :active_record (default), :mongo_mapper and
  # :mongoid (bson_ext recommended) by default. Other ORMs may be
  # available as additional gems.
  require "ckeditor/orm/active_record"

  # Customize ckeditor assets path
  # By default: nil
  config.asset_path = "/assets/ckeditor/"
end

我希望它有所帮助:D!

答案 3 :(得分:1)

ckeditor gem版本3.7.1开始,我仍然没有成功使用资产管道进行生产。但是,我在ckeditor_rails宝石上取得了成功。设置指令位于项目的GitHub页面上,很容易设置。

答案 4 :(得分:0)

对于Ckeditor gem v&gt; 4.0

  1. 在application.js
  2. 中添加以下内容
     //= require_tree ./ckeditor
    
    1. 在app / assets / javascript / ckeditor
    2. 中添加config.js.

      示例config.js

      if(typeof(CKEDITOR) != 'undefined')
      {
        CKEDITOR.editorConfig = function(config) {
          config.uiColor = "#AADC6E";
          config.toolbar = [ [ 'Source', 'Bold' ], ['CreatePlaceholder'] ];
        }
      } else{
        console.log("ckeditor not loaded")
      }