One reason I really enjoy working with Ruby/Rails is because of the Gems. When you want to integrate a Rails app with some 3rd party utilities, it’s pretty likely that you can find existing Gems doing just that for you (80-20 rule here of course).
I need to integrate CKEditor with my Rails app. Guess what? There’s a ckeditor gem for it! Just follow the VERY good readme to get it installed. For a Rails 3.2 app, you literally just need to add 2 lines or 3 lines of code to have it all set up …
Add it to your
Gemfile, and bundle install it
If you want to handle uploads from the editor, stick the following into your
ruotes.rb. I didn’t do this myself, because I’m only after text editing.
At this point, it’s done and working (this is the 80% rule)! However for what I needed (the 20%), I did some customisation.
I’m using the awesome simple_form, so I created a little helper method in my
As you can see, I load a custom
ckeditor_config.js. It basically gives me a custom tool bar. Oh! Don’t forget to add ckeditor_config.js to
config.assets.precompile list in
Now, in any of my view files, I use the helper method like this
All good! Yes, all good on development… Deploy it to staging, it fails miserably. Why? Asset Pipeline again! For Heroku hosted apps (staging and production), I’m using an S3
asset_host for static assets. The CKEditor init script tries to load it from the app’s domain instead…
application.js.erb, so that we can use Ruby code. Make the manifest require self and add some voodoo to set the
Sweet, all good.