2

I am currently using a f.text_area for users to enter text inside a form. Since those entries can be quite long, I want to offer my users to format the input with bold, italic and underlines.

Ideally, users access the formatting options with the common cmd+b, cmd+i and cmd+u.

I know about contenteditable but since this can't be applied to a textarea inside a form but only to a div, this does not seem to work inside a rails form.

What other options are there that play nicely with form_for?

Thierry M.
  • 113
  • 1
  • 15

2 Answers2

3

Ruby on Rails Solutions

  1. rails-ckeditor
  2. Mercury
  3. rails_tiny_mce
  4. redactor-rails

I personally suggest Mercury, there's also a Railscast available for this specific gem :)

I hope this helps!

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
  • I don't want a toolbar at all, I simply want the hotkeys to make text bold and italic – is it possible to customise Mercury like that? – Thierry M. Jan 19 '16 at 08:44
  • Unfortunately no. At least not from what I know. I suggest you then use `https://github.com/galetahub/ckeditor` as it'll be integrated in your form. The gem automatically picks up `simple_form` as well as `formtastic` forms so ease the process :) – Philipp Meissner Jan 19 '16 at 12:20
  • Oh wait, I just saw your comment to the answer that suggests CKEditor. I doubt something like that already lives in a gem. Your best approach is probably using Javascript to apply certain behaviors to selected text. Here's a similar answer that might be what you're looking for: `http://stackoverflow.com/questions/20880271/make-selected-text-bold-unbold`. – Philipp Meissner Jan 19 '16 at 12:22
1

We've implemented CKEditor before - here's how to do it (I can delete if you wish):

#Gemfile
gem 'ckeditor', '~> 4.1.6'

#app/assets/javascripts/application.js
//= require ckeditor/init

#config/initializers/asset.rb
Rails.application.config.assets.precompile += %w(ckeditor/*)

#app/assets/javascripts/ckeditor/config.js
CKEDITOR.editorConfig = function(config) {
  ...
};

The beauty of this gem is it integrates with Paperclip, allowing you to upload files directly to the server (I can provide code if required).

You can also change the toolbar & styling to be anything you like.

Although it's quite bloated, it's the best we've used. The other good one you'll benefit from is redactor, although it's premium and I've not seen a direct integration into Rails.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I don't want a toolbar at all, I simply want the hotkeys to make text bold and italic – is that possible to customise CKEditor like that? – Thierry M. Jan 19 '16 at 08:43
  • 1
    [I believe so](http://stackoverflow.com/questions/13611386/can-i-use-ckeditor-without-a-toolbar) – Richard Peck Jan 19 '16 at 08:54