How to setup TinyMCE for Ruby on Rails

UPDATE: the yui_editor from yahoo might be a better option. especially larsklevan’s yui_editor plugin for rails. it worked the first time, was way easier to setup and tinymce, and is still working great.

see new post on how to setup yui_editor

1. Install the plugin (instructions available on the rails wiki)

  • ruby script/plugin install http://secure.near-time.com/svn/plugins/trunk/tiny_mce/
  • rake tiny_mce:scripts:install
  • Put the following in your views:
  • Add it to whatever controller needs it:
    class MyController < ApplicationController
    uses_tiny_mce

And add this code in your view admin.rhtml or application.rhtml

<% # Include TinyMCE before other JS to avoid problems -%>
<%= javascript_include_tiny_mce_if_used %>
<%= tiny_mce if using_tiny_mce? %>

That’s actually it. It will work really nicely (tested on Rails 2.0.2 and ruby 1.8.6 on localhost)

Next, you’ll want to be able to add images inline style. Here’s a useful tutorial Part 1 and Part 2 for that.

Make sure foil XSS attacks MAKE A WHITELIST. Technoweenie is awesome.

Another helpful tutorial.


Keep in mind, you’ll need to restart your server.

Also, here’s a more custom setup for the uses_tiny_mce call in your controller. This is the setup I prefer.

uses_tiny_mce(:options => {:theme => 'advanced',
                             :browsers => %w{msie gecko},
                             :theme_advanced_toolbar_location => "top",
                             :theme_advanced_toolbar_align => "left",
                             :theme_advanced_resizing => true,
                             :theme_advanced_resize_horizontal => false,
                             :paste_auto_cleanup_on_paste => true,
                             :theme_advanced_buttons1 => %w{bold italic strikethrough separator justifyleft justifycenter justifyright indent outdent separator bullist numlist separator link unlink separator undo redo },
                             :theme_advanced_buttons2 => [],
                             :theme_advanced_buttons3 => [],
                             :plugins => %w{contextmenu paste}},
                :only => [:new, :edit, :show, :index])

UPDATE JULY 10TH, 2008
You might have some issues with remote_for forms with tinymce. To solve the problem do something like this:

<% form_remote_for([:admin, Snippet.new], :before => "tinyMCE.triggerSave(true,true)") do |f| %>

Better yet, take a look at this awesome tutorial for how to use tinyMCE with AJAX in Rails

Comments

  1. RoR Tuesday June 13, 2008 | June 10, 2008

    [...] How to setup TinyMCE for Ruby on Rails [...]

  2. [...] I was using tinymce on my rails app along with the plugin. I’ve since moved to using the yui_editor through larslevan’s [...]

  3. renuka | September 13, 2008

    Hi,

    I want to use tinymce but i have one problem like in my page there are so many textarea and i want to use tinymce for 2 textarea only so can you say me hows that possible?

  4. scott | September 13, 2008

    renuka. I had the same problem. I solved it by using the yui editor setup instead - http://scottmotte.com/archives/196.

    You can specify a class like :class => ‘blog_post’, that shows the text editor. And without that class the textarea will not include the editor buttons.

    Just follow the readme here to get it setup: http://github.com/larsklevan/yui_editor/tree/master

  5. renuka | September 14, 2008

    Thank you very much scott its working :)