Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure CKEditor in Rails 3.1 (gem + Asset Pipeline)

I've successfully configured the ckeditor gem from https://github.com/galetahub/ckeditor on my Rails 3.1 app. My problem now is that I can't figure out how to configure the CKEditor. The files that are used according to the Readme simply don't exists in a Rails 3.1 application with the asset pipeline enabled.

like image 367
wintersolutions Avatar asked Feb 04 '12 16:02

wintersolutions


3 Answers

So I got this working yesterday for Rails 4.0 rc1 and Ruby 2.0 by leaving out the CKEDITOR.editorConfig = function( config ){ } part.

My final code in app/assets/javascripts/ckedtior/config.js was

  CKEDITOR.config.toolbar= [
    { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
  ]
like image 44
guy8214 Avatar answered Oct 12 '22 09:10

guy8214


The answer was easy once i've figured out the thrown error message.

/app/assets/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' ] }
    ];
}

This is the important part, place the require_tree (which includes the ckeditor/config.js) after the require for ckeditor: /app/assets/javascript/application.js

//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_tree .
like image 136
wintersolutions Avatar answered Oct 12 '22 09:10

wintersolutions


This is the updated answer to Rails 4.1 with ckeditor 4.1.0 to custom configure the ckeditor toolbar.

In your view, using simple_form, you need to config the input like this example:

_FORM.HTML.ERB

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

In your Javascript assets you need to create a folder called "ckeditor" and in there create a file called "config.js"

../JAVASCRIPTS/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;
};

The config for the application.js is like this, notice that the order of ckeditor and require_tree is important

APPLICATION.JS

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

Now in your ckeditor.rb you should uncomment this line "config.asset_path" and add the path to the config.js file that you created before wich is "/assets/ckeditor/"

../CONFIG/INITIALIZERS/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

I hope it helps :D!

like image 20
Demi Magus Avatar answered Oct 12 '22 10:10

Demi Magus