Add line break to custom error message

Is it possible to add a line break to the middle of a custom error
message?

I have tried:

errors.add(:thesis, "This field is too long." + "<br />" + "You are
using #{field.length} characters out of a permitted #{permitted}.")

errors.add(:thesis, "This field is too long." + "\n" + "You are using
#{field.length} characters out of a permitted #{permitted}.")

And all other possible combinations of the above that I could think of,
but nothing works.

Any help greatly appreciated.

Never tried it, but I am thinking your first option should work if you
sanitize the whole '<br />'. As you posted it, it might be getting
processed and made useless by Rails. Have you checked the source to
see what was the HTML output?

pepe wrote:

Have you checked the source to see what was the HTML output?

Thanks for the answer.
The source reads:

This field is too long. & l t ; br /& g t ;You are using 12600
characters out of a permitted 1000.

(I have put spaces between the characters so that they don't get parsed
when I submit this!)

It seems that rails is sanitizing the output for me.

Any idea how I can get round this?

Adding this to a regular page broke the string in 2 lines on the page:

First line<%= sanitize('<br />') -%>Second line

It's not the same thing but it might work?

First line<%= sanitize('<br />') -%>Second line

I tried:

errors.add(:thesis, "This field is too long. <%= sanitize('<br />') -%>
You are using #{field.length} characters out of a permitted
#{permitted}.")

This produced:
"This field is way too long. <%= sanitize('<br />') -%> You are using
12600 characters out of a permitted 1000."

In the source code:
Publications This field is sway too long.& l t ;%= sanitize('& l t ;br
/& g t ;') -%& g t ;You are using 12600 characters out of a permitted
1000.

I also tried:
errors.add(:thesis, "This field is too long." + <%= sanitize('<br />')
-%> + "You are using #{field.length} characters out of a permitted
#{permitted}.")

This produced a syntax error:
/applicant.rb:505: syntax error, unexpected '<'
...s field is way too long." + <%= sanitize('<br />') -%> + "Y...

Thanks for your help so far.

I tried:

errors.add(:thesis, "This field is too long. <%= sanitize('<br />') -%>
You are using #{field.length} characters out of a permitted
#{permitted}.")

This produced:
"This field is way too long. <%= sanitize('<br />') -%> You are using
12600 characters out of a permitted 1000."

In the source code:
Publications This field is sway too long.& l t ;%= sanitize('& l t ;br
/& g t ;') -%& g t ;You are using 12600 characters out of a permitted
1000.

I also tried:
errors.add(:thesis, "This field is too long." + <%= sanitize('<br />')
-%> + "You are using #{field.length} characters out of a permitted
#{permitted}.")

This produced a syntax error:
/applicant.rb:505: syntax error, unexpected '<'
...s field is way too long." + <%= sanitize('<br />') -%> + "Y...

Trying a few things. But in the meanwhile, you could add 2 separate
messages with 'add_to_base':

errors.add_to_base 'The publications field is too long.'
errors.add_to_base 'You are using...'

I have not been able to find a way of breaking the line, except by
breaking the message in 2 separate ones as I explained before. If you
do, please post the solution. I would like to know. Sorry I couldn't
be of much help.

pepe wrote:

If you do, please post the solution. I would like to know.

Hi Pepe,

so, after a week of trying I finally found the solution and am posting
it here in case it helps anyone else.

Error message in model:

errors.add(field, "This field is too long.\nYou are using
#{self[field].length} characters out of a permitted #{permitted}.")

And in the view:
<%= simple_format(error_message_on :applicant, :thesis) %>

Thanks very much for your help.

http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format

Jim Burgess wrote:

pepe wrote:

If you do, please post the solution. I would like to know.

Hi Pepe,

so, after a week of trying I finally found the solution and am posting
it here in case it helps anyone else.

Error message in model:

errors.add(field, "This field is too long.\nYou are using
#{self[field].length} characters out of a permitted #{permitted}.")

And in the view:
<%= simple_format(error_message_on :applicant, :thesis) %>

You could also probably put a literal "<br>" in the error message, then
use unescape_html.

Thanks very much for your help.

http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format

Best,

Thanks Jim

Just a follow up in case this helps anyone else:

You could also probably put a literal "<br>" in the error message, then
use unescape_html.

As it turns out, simple_format was wrapping everything in < p > tags,
which was causing invalid html to be generated in the error view of my
form.

So, I tried Maren's suggestion and came up with the following:

in environment.rb
require 'cgi'

Error message in model:
errors.add(field, "This field is too long.%3cbr %3eYou are using
#{self[field].length} characters out of a permitted #{permitted}.")

And in the view:
<%= CGI.unescape(error_message_on :applicant, :thesis) %>

This works just fine and produces valid markup in the form's error view.

Thanks Maren.