Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devise/omniauth login with twitter - validation failed: email can't be blank

I've set up authentication in a rails app using devise, following this tutorial. It works great, a user can register and then log in using an email and password.

I now want to add the ability to register/log in using twitter. I'm following the guide from the devise wiki

The example in the wiki uses facebook, I've changed it to use twitter. It manages to go to twitter and request authorisation, so I'm sure I have the basics working. However once it returns to my app I get the following error.

Validation failed: Email can't be blank

I presume it's because I'm failing to provide an email address.

The code I'm using is identical (apart from swapping facebook to twitter) to that in the tutorials, so I'm not posting any samples. Please let me know if you want to see anything.

I'm quite new to rails so I'm not sure how to debug. Any advice on how I should continue?

like image 275
Finnnn Avatar asked Apr 29 '12 18:04

Finnnn


2 Answers

Hey just remove ":validatable" from user model n it will allow blank email.worked for me!!

like image 75
mandar.gokhale Avatar answered Oct 07 '22 15:10

mandar.gokhale


I know this question is old but it still comes up and a quick google search uncovered some very bad advice. So here...

The correct way to make emails optional isn't to remove the validatable interface from your user model, just add this to it instead:

def email_required?
  false
end 

This overrides the method in the 'validatable' class, signalling to the code that email is an optional field, but keeps all the good stuff that validatable includes, like, you know... making sure you've got an actual password and handy things like that. Stuff you want to keep.

Of course, if you need anything anything really wacky, you should take a look at the source code of the validatable interface and copy in the relevant validations and adjust as necessary but for 99.99% of people, the code I've provided above is probably enough.

Hope that helps a weary a googler.

like image 41
ac_ Avatar answered Oct 07 '22 15:10

ac_