mike's Blog
gusto! travel tools
TinyMCE, Spellcheck and Ruby on Rails
Well – we’ve had the need for a spell checker for our blog, comment and review text areas for a while – we initially integrated FCKEditor but I found it’s interface a bit garish and it didn’t work in Safari at all so I went and took a look into TinyMCE again. They have a very simple and nicely implemented (google gmail style) spellchecker that works as a plugin to the TinyMCE framework.
The problem with the plugin is that out of the box it requires PHP. Initially we recompiled lighttpd to enable it to serve up PHP files but the dueling app servers didn’t seem like a good idea for production. So I set out to write a rails implementation against aspell which is one of the supported methods that the plugin uses.
I had already written some homegrown code to talk to aspell and decided that this could be refactored to use the XMLHttpRequest method that TinyMCE used.
It was actually very simple. Just add the spelling.rb module to your /lib directory in Rails which looks like this:
module Spelling
ASPELL_WORD_DATA_REGEX = Regexp.new(/&sw+sd+sd+(.*)$/)
ASPELL_PATH = “aspell-0.60”
def check_spelling(spell_check_text, command, lang)
xml_response_values = Array.new
spell_check_response = `echo ”#{spell_check_text}” | #{ASPELL_PATH} -a -l #{lang}`
if (spell_check_response != ’’)
spelling_errors = spell_check_response.split(” ).slice(1..-1)
if (command 'spell')
for error in spelling_errors
error.strip!
if (match_data = error.match(ASPELL_WORD_DATA_REGEX))
arr = match_data[0].split(' ')
xml_response_values << arr[1]
end
end
elsif (command ‘suggest’)
for error in spelling_errors
error.strip!
if (match_data = error.match(ASPELL_WORD_DATA_REGEX))
xml_response_values << error.split((”, ”, ““)
end
end
end
end
return xml_response_values.join(““)
end
end
then in your application.rb (or wherever you feel like putting it) add this spellcheck action:
def spellcheck
@headers[‘Content-Type’] = ‘text/xml’
@headers[‘charset’] = ‘utf-8’
suggestions = check_spelling(params[:check], params[:cmd], params[:lang])
xml = ”<?xml version=”1.0” encoding=”utf-8” ?>
render :text => xml
return
end
Note in the spellcheck action there’s a parameter called :tinymce_id. That’s gets created in a route because tinymce actually passes this variable with the “id” which confuses Rails since it has special meaning.
Here at gusto we don’t use routes.rb because the amount of URL rewriting we do required it’s own controller that we call RoutingController. Here’s the code that renders the spellcheck action:
params[:tinymce_id] = params[:id]
render_component(:controller => ‘gusto’, :action => “spellcheck”, :params => params)
You will need to also either trap for the /tinyspell.php request or do what we did and modify editor_plugin.js to point to a method called ”/spellcheck” which we trap for in our routing controller. Either way should work fine.
Also I had to fix some bugs in the spellchecker and TinyMCE to get this all to work in IE6 and IE7 (gotta love Internet Explorer for wasting your time if you’re web developer). You can read about that here:
http://tinymce.moxiecode.com/punbb/viewtopic.php?id=5055
All in all though I think the combination of TinyMCE and Ruby on Rails provides an excellent spell checker solution that works in all the major browsers and except for the IE nonsense was trivial to integrate.
Feel free to ask questions if you need help getting it setup.
Mike
Comments on this post:
Posted by banslakhil on Friday June 27, 2008 at 02:05AM
Mike, I do also need your spelling.rb .
I could not able to mae it working by copying from your site.
Is there any other link where I can download plain text file?
Many thanks
Posted by macsig on Wednesday February 20, 2008 at 06:23PM
Hello Mike,ou could you please email me the text as well?
thanks and have a nice day!
Posted by colin on Friday December 21, 2007 at 12:08PM
I would love a copy of the code too as the formatting has borked the published version :) thanks in advance.
Posted by wiseleyb on Saturday December 08, 2007 at 06:10PM
Mike this looks - any chance you could email the text as well?
Posted by rightondev on Monday November 12, 2007 at 02:58PM
Hey Mike, the spellchecker is great.
Can you send me an email with the text as well? The html formating seems to have hosed it badly.
Posted by jakesutton on Tuesday September 11, 2007 at 05:00PM
Mike, the code for the spelling.rb module got kind of hosed. Can you link to plain text / provide a download / send me a copy?
I'd sure appreciate it. I'm dying to get this working in my rails app.
Reply by jakesutton on Tuesday September 11, 2007 at 05:12PM
Excellent. I replied via email. Thanks!
Posted by nicksmooth on Wednesday November 15, 2006 at 01:25PM
Mike I am having problems with the code above do to the HTML characters that aren't correct quotes and such. I tried to fix it up when I copied it, but I am missing something. Could you send me this code or something. I really like the spellchecker on your site I am trying to get it running in a couple of my RoR apps. Thanks.
Ooops – forgot to mention that you'll need aspell installed and if you want to use multiple languages you'll need to install the appropriate dictionaries to get it to work correctly with TinyMCE.
Enjoy the full gusto! experience with these tools:
- mike's travel guides
- mike's profile
- mike's friends (60)
- mike's blog (3)
- Search My Blog Posts:
- Search My Blog Posts:
- mike's photos (189)
- mike's reviews (24)
- mike's bookmarks (180)


Posted by JitendraRai on Wednesday July 02, 2008 at 01:37AM
Hi mike,it seems breaked html on the blog.Can you please send me a copy of spelling.rb on my mail...Thanx in advance