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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Sep 21, 2:57 am, Jim Burgess <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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?> 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? > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> First line<%= sanitize(''<br />'') -%>Second lineI 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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...'' -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Sep 21, 5:52 pm, Jim Burgess <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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_formatBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Jim On Sep 27, 3:29 pm, Jim Burgess <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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) %> > > Thanks very much for your help. > > http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.