ruby on rails – getting error messages, authlogic email validation March 15, 2009
Posted by ficial in techy.trackback
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
else
@user.errors.each_full { |msg| logger.debug("ERROR: "+msg) }
render :action => :new
end
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.







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:
http://rdoc.info/projects/binarylogic/authlogic
Ah – excellent! That’s much better, thanks!
Just curious, why do you want to disable the validation of uniqueness on the email field?
[...] too ran into some trouble before discovering that my beloved AuthLogic is validating my email field. The validation is a good [...]