Skip to content
galetahub edited this page Sep 13, 2010 · 3 revisions

Rails CKEditor integration plugin with SWFUpload support

CKEditor is a text editor to be used inside web pages. It’s a WYSIWYG editor, which means that the text being edited on it looks as similar as possible to
the results users have when publishing it. It brings to the web common editing features found on desktop editing applications like Microsoft Word and OpenOffice.

Because CKEditor is licensed under flexible Open Source and commercial licenses, you’ll be able to integrate and use it inside any kind of application.
This is the ideal editor for developers, created to provide easy and powerful solutions to their users.

CKEditor version: 3.1
SWFUpload version: 2.2.0
Rails version: 2.3.x

ckeditor.com
swfupload.org

Demo appication:
rails-ckeditor-demo-app

Install

./script/plugin install git://github.com/galetahub/rails-ckeditor.git

rake ckeditor:install

rake ckeditor:config

Last rake generated file config/ckeditor.yml:


development: 
  swf_file_post_name: "data"
  swf_image_file_types_description: "Images"
  swf_image_file_types: "*.jpg;*.jpeg;*.png;*.gif"
  swf_image_file_size_limit: "5 MB"
  swf_image_file_upload_limit: 10
  swf_types_description: "Files"
  swf_file_types: "*.doc;*.wpd;*.pdf;*.swf;*.xls"
  swf_file_size_limit: "10 MB"
  swf_file_file_upload_limit: 5
  public_uri: "/uploads"
  public_path: "public/uploads"
  file_manager_uri: "/ckeditor/files"
  file_manager_upload_uri: "/ckeditor/create?kind=file"
  file_manager_image_upload_uri: "/ckeditor/create?kind=image"
  file_manager_image_uri: "/ckeditor/images"

For attachment_fu: swf_file_post_name: "uploaded_data"

Usage

Basically include this in the page you wish to use the editor in


  <%= javascript_include_tag :ckeditor %>

Then instead of the normal textarea helper from Rails use this one


  <%= ckeditor_textarea("object", "field", :width => '100%', :height => '200px') %>

FormBuilder helper for more usefully

  
  <% form_for :page, :url => pages_path do |form| -%>
    ...
    <%= form.cktext_area :notes, :toolbar=>'Full', :width=>'400px', :heigth=>'200px' %>
    ...
    <%= form.cktext_area :content, :swf_params=>{:assetable_type=>'User', :assetable_id=>current_user.id} %>
    ...
  <% end -%>

Support options
 
  :cols    # Textarea cols
  :rows    # Textarea rows
  :width   # Editor width
  :height  # Editor height
  :class   # Textarea css class name
  :toolbar # Toolbar name
  :skin    # Editor skin
  :language # Editor language
  :swf_params # SWFUpload additional params

Check public/javascripts/ckeditor/config.js for config default options.
Modify public/javascripts/ckeditor/contents.css – this stylesheet use editor

AJAX

To use a remote form you need to do something like this


  <%= form_remote_tag :url => @options.merge(:controller => @scaffold_controller),
                    :before => Ckeditor_before_js('note', 'text') %>
<%= ckeditor_textarea( “note”, “text”, :ajax => true ) %>
<%= end_form_tag %>

If you forget to put in the :before it won’t work, you can also use the Ckeditor_form_remote_tag described below

Multiple Editors in a form

To create a form using multiple editors use the Ckeditor_form_remote_tag helper and pass the :editors option. This takes an hash of model symbol keys with each having
an array as its value. The array should contain the list of fields that will have editors attached to them.


  <%= ckeditor_form_remote_tag :url => @options.merge(:controller => @scaffold_controller),
                              :editors => { :multinote => ['text1', 'text2'] } %>
<%= ckeditor_textarea( “multinote”, “text1”, :ajax => true ) %>
<%= ckeditor_textarea( “multinote”, “text2”, :ajax => true ) %>
<%= end_form_tag %>

File uploads

We recommend using a paperclip plugin for file storage and processing images. Controller ../rails-ckeditor/app/controllers/ckeditor_controller.rb has actions
for displaying and uploading files. It uses classes Picture and AttachmentFile, who are descendants of the Asset class. So, your project must have these classes.

http://github.com/thoughtbot/paperclip

For paperclip:
ActiveRecord model Asset (asset.rb):


class Asset < ActiveRecord::Base
  belongs_to :user
  belongs_to :assetable, :polymorphic => true
def url(*args)
data.url(*args)
end
alias :public_filename :url
def filename
data_file_name
end
def content_type
data_content_type
end
def size
data_file_size
end
def path
data.path
end
def styles
data.styles
end
def format_created_at
I18n.l(self.created_at, :format=>"%d.%m.%Y %H:%M")
end
def to_xml(options = {})
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
xml.tag!(self.type.to_s.downcase) do
xml.filename{ xml.cdata!(self.filename) }
xml.size self.size
xml.path{ xml.cdata!(self.url) }
xml.styles do
self.styles.each do |style|
xml.tag!(style.first, self.url(style.first))
end
end unless self.styles.empty?
end
end

end

ActiveRecord model AttachmentFile (attachment_file.rb):


class AttachmentFile < Asset
  has_attached_file :data,
                    :url => "/assets/attachments/:id/:filename",
                    :path => ":rails_root/public/assets/attachments/:id/:filename"
validates_attachment_size :data, :less_than => 10.megabytes

end

ActiveRecord model Picture (picture.rb):


class Picture < Asset
  has_attached_file :data,
                    :url  => "/assets/pictures/:id/:style_:basename.:extension",
                    :path => ":rails_root/public/assets/pictures/:id/:style_:basename.:extension",
	            :styles => { :content => '575>', :thumb => '100x100' }
validates_attachment_size :data, :less_than => 2.megabytes
def url_content
url(:content)
end
def url_thumb
url(:thumb)
end
def to_json(options = {})
options[:methods] ||= []
options[:methods] << :url_content
options[:methods] << :url_thumb
super options
end

end

More info in ../rails-ckeditor/examples/models.
Do not forget about migration ../rails-ckeditor/examples/migrations.

TODOs

1. Add support for choose filemanager storage
2. More integration upload system