# Providing HTML in an error message generated by validation

**URL:** <https://www.ruby-forum.com/t/providing-html-in-an-error-message-generated-by-validation/116240>\
**Category:** Rails\
**Created:** [October 3, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/providing-html-in-an-error-message-generated-by-validation/116240 "2007-10-03T19:22:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 3, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/providing-html-in-an-error-message-generated-by-validation/116240/1 "2007-10-03T19:22:41Z")

</div>

All,

I have some validation that I’m doing in a model.

As part of the error message, I would like to provide an HTML link.

What is the best approach to handling this?

APPROACH 1: One way is to

include ActionView::Helpers::UrlHelper  
include ActionView::Helpers::TagHelper

in my model so that I can use link\_to to generate my link (obv. I could  
hard code HTML as well).

But anyway that feels crappy - too much view stuff in the model.

APPROACH 2: A better seeming - approach would be to encapsulate the  
generation of the link in a helper method and call that in my model  
where I generate the error message.

How do I get access to one of my helpers from a model class?

APPROACH 3: Put in some kind of place-holder when I generate the error  
in the model and then replace that on the fly in the controller before I  
render the error.

Any thoughts on this?

In general, I think it’s easy to forget that when you put error message  
generation in the model, you are generating view artifacts, it just may  
not feel that way.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 3, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/providing-html-in-an-error-message-generated-by-validation/116240/2 "2007-10-03T21:15:48Z")

</div>

I have no problem with error generation in the model. It’s nice to see  
an  
array of error messages in the model.

I would say you make your own display for it tho…

\<%=special\_error\_messages\_for @foo %\>

and in the helper, inspect the errors collection that comes from the  
model  
and then display appropriate values. If you look at the rails source,  
it  
specifically says that error\_messages\_for is really just an example and  
you  
should do your own.

Remember, the model only puts messages in an array. Your view formats  
them… I don’t consider those view artifacts any more than I consider a  
total for an invoice to be one… it’s 200.0 in the model, but in the  
view  
I use the number\_to\_currency helper 🙂

Just my opinion tho… feel free to disagree. This is how I’ve handled  
it  
before tho.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 3, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/providing-html-in-an-error-message-generated-by-validation/116240/3 "2007-10-03T21:52:31Z")

</div>

I ended up doing this:

1. setting a placeholder in my error message in the model, like so:

2. In my view, I sub in the output of a helper (method called  
generate\_appropriate\_link), like so:

(come to think of it, I could probably hide most of this in the helper  
method)

and then later calling error\_messages\_for(:obj\_of\_interest)

This allowed me to take advantage of the URL helper stuff more easily.

===========

To respond to Brian’s post, I guess I am a little hung up on the fact  
that error “messages” are being set in the model. Obviously, you have  
the power to do anything to that message that you want when you render  
the view.

The error message (set in the model) does two things:

1. by its existence it indicates that an attribute is “in error”, and
2. provides a default rendering for the information about the error for  
the view.

So I think that the built-in error message stuff _is_ providing view  
artifacts in the sense that you get a default representation of error in  
the view.

Although, to be fair, this is the nature of the Rails Errors object :).  
Which I think I am arguing is blurring the boundaries between view and  
model. But that’s ok.

===============

It works 🙂

Wes
