Last Updated: May 15, 2019
·
922
· ireneuszskrobis

Displaying formtastic styled errors for ckeditor in ruby on rails application

If you are using formtastic and ckeditor gems in your application, then you have probably noticed that on failed update/create all normal fields are bordered with red line and errors are displayed for them. Unfortunately your ckeditor fields don’t behave that way... To make it work you can add this small method to application_helper.rb:

def ckeditor_input(form, field)
 result = form.cktext_area field
 if form.object.errors.has_key?(field)
  errors = '<div class="control-group error"><span class="help-inline">'
  errors += form.object.errors[field]*", "
  errors += '</span></div><br/>'
  return ("<div class='ck_error'>".html_safe) + result + ("</div>#{errors}".html_safe)
 end
 result
end

and add some styles, for example:

.ck_error {
    border-color: $red;
  color: $red;
  vertical-align: 3px;
  .help-inline {
    padding-left: 160px;
  }
}

Now in the view you can use ckeditor_input like this:

= semantic_form_for @note do |f|
 = f.input :title 
 = ckeditor_input(f, :content)
 = f.action :submit