jump to navigation

ruby on rails – getting error messages, authlogic email validation March 15, 2009

Posted by ficial in techy.

I’ve been working on a RoR project for a bit, and it’s going slowly.  The big problem is that I spend so much time fighting the system, especially in the testing framework. Just now I spent about 2 hours tracking down the cause of an error. The heart of the problem was not the error itself, but getting any info beyond “an error occurred”.

The problem was that a save on a user object was failing, but I had no idea why because the log files had no useful info. I finally found something useful at http://dizzy.co.uk/ruby_on_rails/cheatsheets/active-record-validations#low_level_validations. It turns out that ActiveRecord objects store their errors, and you can use logger methods (see http://www.martyandrews.net/blog/2007/09/logging_in_ruby_on_rails.html for more details) to put them in your log file. Thusly

if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
@user.errors.each_full { |msg| logger.debug("ERROR: "+msg) }
render :action => :new

I was able to see that the problem was with the email attribute of the user object. I’m using AuthLogic, which has generally been a fantastic user / session / authentication system (I found it much easier to use than restful_authentication, and the Authlogic tutorial and example app are really good). However, in this case it was a real pain. The problem turned out to be non-unique email addresses.

It turns out that Authlogic by default makes sure the email is unique. While this is described in the reset password tutorial, it’s not in the setup one. Not knowing this landmine was waiting, I’d added an email field to my user object, and then had the create user test fail with out any explanation. The above code finally showed me the error message explaining that I had a non-unique email. From there I did a bit of research and found that on the user object I can use
acts_as_authentic :validate_email_field => false
to disable that check, and voila, problem solved.



1. Bachir El Khoury - July 3, 2009

This way your emails will not valid at all.

You can simply use :validates_uniqueness_of_email_field_options=>false
instead, to disable that criteria.

check out the docs:

2. ficial - July 6, 2009

Ah – excellent! That’s much better, thanks!

3. Hari - August 19, 2009

Just curious, why do you want to disable the validation of uniqueness on the email field?

4. game, game. repeat. » How To Permit A Blank Email Field With AuthLogic - September 9, 2009

[…] too ran into some trouble before discovering that my beloved AuthLogic is validating my email field. The validation is a good […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: