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?
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...
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.
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.