You are currently viewing an archive of this site. To view new content and see what I’ve been up to lately please check the main page at http://www.aidanf.net
Adding a rich text editor to your application enables users to markup their input text without having to know a markup syntax such as markdown or html. This page describes how to easily add this functionality to your rails application.
There are several free javascript rich text editors that can be incorporated into your application. These work by providing a javascript WYSIWYG interface that allows the user to generate the required html for each element that is enabled in the interface. So for example, if the user selects “XYZ” and presses the bold button, this generates the text <b>XYZ</b> which is saved to the database.
Since using these editors allows users to create html that will be input into the database, make sure that you sanitize the html when you are rendering it back from the database. You can use rails’ sanitize method or since you probably want to allow several tags you could use this Ruby sanitize function with the okTags parameter set to whatever set of tags you want to allow. Alternatively check out the whitelist recipe in the rails recipes book.
widgEditor is a small simple lightweight editor. It provided basic rich functions such as bold, italics, links, lists, images and headings. Its very easy to get setup with your rails application.
Here is an example of what you might have in your view file:
<%= stylesheet_link_tag 'widgEditor' %> <%= javascript_include_tag "widgEditor" %> <%= text_area 'node', 'content', :cols => "50", :rows=>"3", :class=>"widgEditor" %>
TinyMCE is a much more full featured rich text exitor. Check out the demo to see all the features that can be enabled for it. It is easy to incorporate TinyMCE into your rails application.
All your text area fields will now be TinyMCE editor instances. Below is an example of what you might have in your layout file. It only enables a small subset of the available TinyMCE widgets.
<%= javascript_include_tag 'tiny_mce/tiny_mce' %> <script language="javascript" type="text/javascript"> tinyMCE.init({ mode : "textareas", theme : "advanced", convert_urls : false, plugins : "emotions,preview", theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough, bullist,numlist,link", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", extended_valid_elements : "a[name|href|target|title|onclick],img[class|src| border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name], hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]" }); </script>
In your view create a text area.
<p><label for="blog_content">Content</label><br/> <%= text_area 'blog', 'content', :cols => "50" %></p>
TinyMCE includes quite a lot of javascript files. It is a good idea to include these files only on pages that actually use the rich text editor. This will save on bandwidth and make for quicker page loads for the pages that aren’t using the rich text editor. You could do this by having a separate layout for the part of the application that uses the TinyMCE or by adding a condition in your layout file that checks the controller name before deciding to load it.
Another option worth investigating is FCKEditor. I haven’t tried this but it looks a bit more complicated that those discussed above.
You can now integrate TinyMCE using the TinyMCE plugin created by Blake Watters. See John Wulff’s tutorial for more details.
Thank you for providing this excellent review and usage of WYSIWYG editors and showing how easy it is to add them to you rails app.
Excellent article! I'll definitely be coming back to this when I start the admin section of my blog.
For "widgEditor", I do think it's a very good rich text editor, while I want know how to setup a db column to handle the rich text field. Appreciate your advise. Thank you
Terry,
Anything that accepts text input would work. CHAR, VARCHAR, TEXT. Having said that, if used for anything large, you'll want this to be of type TEXT since I believe that VARCHAR tops out at 8000 characters.
There seems to be two potential pitfalls using widgEditor.
The latest version on date of writing requires that you need to have an "id" attribute also on each textbox you use it on (unlike your example).
Also, if you have an "onload" attribute on your "body" element, the editor seems to refuse to start.
But otherwise, great article! Thank you! :)
Implemeting FCKEditor in RoR applications is realy simple.
Put
<%= javascript_include_tag "fckeditor" %>
in header of .rhtml file where is textarea.
then put this:
window.onload = function(){
var oFCKeditor = new FCKeditor( 'fck' ) ;
oFCKeditor.ReplaceTextarea() ;
}
and finnaly texarea:
<%= text_area 'article', 'text', :id => 'fck' %>
In the case of widgEditor you have to add a method in helper that is:-