Providing HTML in an error message generated by validation

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

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 :slight_smile:

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

I ended up doing this:

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

    errors.add_to_base(“Click for more info.”)

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

    @obj_of_interest.errors.each_full do |err_msg|
    err_msg.sub!("", generate_appropriate_link)
    end

(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 :slight_smile:

Wes