Is it possible to skip confirmation email and confirm user?

9,245 views
Skip to first unread message

Christian Fazzini

unread,
Sep 27, 2010, 4:56:19 PM9/27/10
to Devise
When doing the following:

user = User.create(
:first_name => 'admin',
:last_name => 'admin',
:email => 'foo...@email.com',
:password => 'password1',
:password_confirmation => 'password1'
)

# Confirm the user for Devise
user.confirm!

The above creates the user in the db and confirms the user. However,
in the background, Devise also sends out an email, along the lines of
"Welcome foo...@email.com! You can confirm your account through the
link below: Confirm my account".

Is it possible to skip the confirmation email and just confirm this
user for this instance?

Carlos Antonio da Silva

unread,
Sep 27, 2010, 5:03:33 PM9/27/10
to plataforma...@googlegroups.com
Yes it is: call skip_confirmation! and then save the record

--
At.
Carlos A. da Silva

Christian Fazzini

unread,
Sep 27, 2010, 5:10:57 PM9/27/10
to Devise
You mean like this:

user = User.create(
:first_name => 'admin',
:last_name => 'admin',
:email => 'foo...@email.com',
:password => 'password1',
:password_confirmation => 'password1'
).skip_confirmation!

On Sep 28, 5:03 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Yes it is: call skip_confirmation! and then save the record
>
> http://github.com/plataformatec/devise/blob/master/lib/devise/models/...

Carlos Antonio da Silva

unread,
Sep 27, 2010, 5:18:13 PM9/27/10
to plataforma...@googlegroups.com
IIRC, skip_confirmation! does not save the record, so you'd have to do sth like:

user = User.create(params here)
user.skip_confirmation!
user.save!

Christian Fazzini

unread,
Sep 27, 2010, 6:56:48 PM9/27/10
to Devise
Nope, that wont work. Devise sends the email when the user is created.
I am still getting confirmation emails. So I don't think it ever
reaches skip_confirmation!

I think .create triggers the email to be sent

On Sep 28, 5:18 am, Carlos Antonio da Silva

Christian Fazzini

unread,
Sep 27, 2010, 7:02:37 PM9/27/10
to Devise
On the other hand,

user = User.new(params here)
user.skip_confirmation!
user.save!

Works! :-)

On Sep 28, 5:18 am, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Sep 27, 2010, 7:03:26 PM9/27/10
to plataforma...@googlegroups.com
Sure, we were using User.create =D

Christian Fazzini

unread,
Sep 27, 2010, 7:19:29 PM9/27/10
to Devise
Ok this seems to work for seeds.

Now I am trying to authenticate the user through facebook. I get this
error:

NoMethodError in Devise/oauth callbacksController#facebook
undefined method `persisted?' for true:TrueClass

In my user model, I have:

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = ActiveSupport::JSON.decode(access_token.get('https://
graph.facebook.com/me?'))

if user = User.find_by_email(data['email'])
user
else
# Create an user with a stub password.
user = User.new(
:email => data['email'],
:first_name => data['first_name'],
:last_name => data['last_name'],
:password => 'password123',
:password_confirmation => 'password123'
)
user.skip_confirmation!
user.save!
end
end

However, with the following code below, everything seems to work...
Why is this error coming up?

Arent both methods the same?

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = ActiveSupport::JSON.decode(access_token.get('https://
graph.facebook.com/me?'))

if user = User.find_by_email(data['email'])
user
else
# Create an user with a stub password.
user = User.create!(
:email => data['email'],
:first_name => data['first_name'],
:last_name => data['last_name'],
:password => 'password123',
:password_confirmation => 'password123'
)
end
end

On Sep 28, 7:03 am, Carlos Antonio da Silva

Alex

unread,
Sep 27, 2010, 7:31:21 PM9/27/10
to plataforma...@googlegroups.com
The stub method that is given for git and facebook when I saw it in
some blogs, is incorrect, I believe.

You need to return 'user'.
user.save! returns true on success...

To fail gracefully, you return a User instance that you have not saved
to the database, so the version of save that raises an exception is
probably not what you want.

-Alex

Christian Fazzini

unread,
Sep 28, 2010, 2:56:59 AM9/28/10
to Devise
Thanks Alex. Yea, I now have it as:

user = User.new(
:email => data['email'],
:first_name => data['first_name'],
:last_name => data['last_name'],
:password => 'password123',
:password_confirmation => 'password123'
)
user.skip_confirmation!
user.save!

user

I am returning the user object. This seems to work now.

By the way, when I try to use Devise.friendly_token, as such:
User.create!(:email => data['email'], :password =>
Devise.friendly_token), I always get "password is too long, maximum is
20" (or similar message). It seems Devise.friendly_token as given in
the git examples is wrong?

http://github.com/plataformatec/devise/wiki/OAuth2:-Overview
http://github.com/plataformatec/devise/wiki/OAuth2:-Testing

I tried increasing the password limit of Devise to 50 characters, and
Devise.friendly_token still provides a longer type of password...

FYI, I am following: http://stjhimy.com/posts/14-allowing-devise-login-with-facebook-account

On Sep 28, 7:31 am, Alex <x37v.a...@gmail.com> wrote:
> The stub method that is given for git and facebook when I saw it in
> some blogs, is incorrect, I believe.
>
> You need to return 'user'.
> user.save! returns true on success...
>
> To fail gracefully, you return a User instance that you have not saved
> to the database, so the version of save that raises an exception is
> probably not what you want.
>
> -Alex
>
> On Mon, Sep 27, 2010 at 4:19 PM, Christian Fazzini
>

Carlos Antonio da Silva

unread,
Sep 28, 2010, 7:22:38 AM9/28/10
to plataforma...@googlegroups.com
I guess friendly_token is 60 chars lenght, so you could do sth like: Devise.friendly_token[0,20]

Alex

unread,
Sep 28, 2010, 12:09:28 PM9/28/10
to plataforma...@googlegroups.com
I believe you want to use the non bang version of save as well, this
way you return an unsaved model if save fails, you don't throw an
exception.

-Alex

Reply all
Reply to author
Forward
0 new messages