Writing more advanced activerecord error messages

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

Why not just use validates_uniqueness_of, with a custom :message ?

custom-err-msg is handy for custom messages:
http://github.com/gumayunov/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.

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
[email protected]

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

Thanks for your input.
–Rob

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

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
[email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs