# Writing more advanced activerecord error messages

**URL:** <https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525>\
**Category:** Rails\
**Created:** [January 29, 2010, 10:56am UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525 "2010-01-29T10:56:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 10:56am UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/1 "2010-01-29T10:56:52Z")

</div>

Hi,  
I want to be able to have a more detailed friendly message for users  
when they register with an email address thats already taken.

At the moment I have written a small validation method to produce what  
I want, personally I dont like it because I’ve had to include modules  
that shouldn’t really be there (ActionController::UrlWriter and  
ActionView::Helpers)

I was considering extending the error\_message\_for method to check for  
error messages on fields against a list stored in a constant then  
replacing them in the errors array, but I dont really like that  
either.

Does anyone know of a plugin or any suggestions on how I could make  
this cleaner?  
Here’s what I have so far.

```
class User < ActiveRecord::Base
  include ActionController::UrlWriter
  include ActionView::Helpers

  validate :unique_email_address
...

def unique_email_address
      if self.class.find_by_email(email)
        errors.add(:email, "The email address #{email} has already
been used. Please use a different email address or #{link_to('Sign
in',login_path)}.")
      end
    end
...
end
```

Thanks  
–Rob

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 2:11pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/2 "2010-01-29T14:11:12Z")

</div>

Why not just use validates\_uniqueness\_of, with a custom :message ?

custom-err-msg is handy for custom messages:

> **[GitHub - gumayunov/custom-err-msg: Fork of...](https://github.com/gumayunov/custom-err-msg)**
>
> Fork of svn://rubyforge.org/var/svn/custom-err-msg - GitHub - gumayunov/custom-err-msg: Fork of svn://rubyforge.org/var/svn/custom-err-msg

I know that doesn’t answer your URL issue, but it would seem a simpler/  
tidier solution even if you still wanted to include the link. I prefer  
to just leave navigation out of notices.

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/3 "2010-01-29T14:24:34Z")

</div>

Rob A. wrote:

> Hi,  
> I want to be able to have a more detailed friendly message for users  
> when they register with an email address thats already taken.
> 
> At the moment I have written a small validation method to produce what  
> I want, personally I dont like it because I’ve had to include modules  
> that shouldn’t really be there (ActionController::UrlWriter and  
> ActionView::Helpers)

If you need a URL in your error message, then this is the proper way, I  
think. It’s not true that UrlWriter “shouldn’t really be there” if it’s  
necessary for the functionality you need.

Of course, to some extent this is a design flaw of Rails: validations  
should certainly be handled by the model, but I find myself thinking  
that error messages should have been in the controller…

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 3:06pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/4 "2010-01-29T15:06:49Z")

</div>

Keavy, Indeed, it would be simpler, but messages with an actions  
provide a much better way to persuade customers into doing certain  
actions and in turn providing them with a better experience, rather  
than them leaving the site because something didn’t work.

Marnen, I also find myself thinking that, if the consensus is that  
including the necessary modules is OK even if their functionality  
isn’t usually meant for models, then I’m happy to leave it be 🙂

Thanks for your input.  
–Rob

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/5 "2010-01-29T15:19:06Z")

</div>

Although, using a Proc for the :message using that plugin could be an  
option, ill test it out and see if i can include links in the string  
returned by the Proc.

Thanks again  
–Rob

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [January 29, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/6 "2010-01-29T15:35:28Z")

</div>

Rob A. wrote:

> Although, using a Proc for the :message using that plugin could be an  
> option, ill test it out and see if i can include links in the string  
> returned by the Proc.

I don’t see why not. It’s just a string.

> Thanks again  
> –Rob

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Rob\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rob\_A](https://www.ruby-forum.com/u/Rob_A)\
**Post date:** [February 3, 2010, 12:52pm UTC](https://www.ruby-forum.com/t/writing-more-advanced-activerecord-error-messages/182525/7 "2010-02-03T12:52:59Z")

</div>

Just to update, still struggling with this.  
It seems now I’m being caught about using both ‘login\_path’ and  
‘link\_to’  
because both use different custom url\_for methods  
`login_path` from `ActionController::UrlWriter`  
and `link_to` from `ActionView::Helpers::UrlHelper`

Seems to be some sort of conflict with both these libraries.

Might have to scrap this whole idea and write markdown or something in  
my messages and replace the error\_message\_for method to generate the  
HTML

–Rob
