Never tried it, but I am thinking your first option should work if you
sanitize the whole ‘ ’. 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(’ ’) -%>
You are using #{field.length} characters out of a permitted
#{permitted}.”)
This produced:
“This field is way too long. <%= sanitize(’ ’) -%> 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(’ ’)
-%> + “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(’ ’) -%> + "Y…
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 “ ” 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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.