How do you skip user confirmation in development in devise.
I have set up the production environment to send emails with SendGrid, but now I have done that it won't let me log in.
Thanks for your time!
create User in console:
user = User.create(
:first_name => 'admin',
:last_name => 'admin',
:email => 'foo...#email.com',
:password => 'password1',
:password_confirmation => 'password1'
).skip_confirmation!
# Devise :doc:
# If you don't want confirmation to be sent on create, neither a code
# to be generated, call skip_confirmation!
or in model:
after_create :skip_conf!
def skip_conf!
self.confirm! if Rails.env.development?
end
Another way:
User.new(email: 'xx#xx.xx', password: '12345678').confirm!
BTW, you can skip :password_confirmation at all, because #confirm! skips validation if record does not persisted yet.
Related
I am trying to create a devise user using rails console with the below method
User.create!({:email => "abc#gmail.com", :roles => ["admin"], :password => "abc123", :password_confirmation => "abc123" })
I am not able to understand If the user is created or not. After I hit enter command I do not get any respond. I did user.save and then did u.all but it shows nothing and I am not able to login.
Please suggest what's wrong.
Edit: I am trying below as well but it is not working
#user = User.new(:email => 'test#example.com', :password => 'password', :password_confirmation => 'password')
#user.save
Thanks
UPDATED:
You are missing the {} in your User.new()
user = User.new({email: 'test#example.com', password: 'password', password_confirmation: 'password'})
user.save
Do you have :confirmable option on? If yes, do:
user = User.new({email: 'test#example.com', password: 'password', password_confirmation: 'password'})
user.skip_confirmation!
user.save
I am using devise gem and while creating user I want to skip confimation! and skip confimation email for example:
User.create(:first_name => "vidur", :last_name => "punj").confirm!.skip_confirmation!
But it skips only confirmation and doesn't skip sending confirmation email. Can any one give me an idea to skip both.
You need to call skip_confirmation! before you save the record.
Try
user = User.new(:first_name => "blah")
user.skip_confirmation!
user.save
You are calling User.create before skip_confirmation!, you need to call User.new and user.save later.
Try
user = User.new(:first_name => "vidur", :last_name => "punj")
user.skip_confirmation!
user.save!
set the confirmed_at field
User.create!(confirmed_at: Time.now, email: 'test#example.com', ...)
useful in seeds.rb
User.create_with(name: "Mr T", company: "Comp X", password: 'rubyrubyruby', password_confirmation: 'rubyrubyruby', confirmed_at: Time.now).find_or_create_by!( email: 'test#example.com')
Got the solution:
#user=User.new(:first_name => "vidur", :last_name => "punj")
#user.skip_confirmation!
#user.confirm!
#user.save
If you are confused where to write skip_confirmation! method in controller as you have not generated devise controllers yet then:
Write this in your User model
before_create :my_method
def my_method
self.skip_confirmation!
end
Now simply use:
user = User.new(:first_name => "Gagan")
user.save
after_create: :skip_confirmation_notification
- check here
If you want to skip_confirmation_notification on certain condition only, use a proc
If you don't need confirmations at all, you can remove the :confirmable symbol in your model.
Every time i reset & seed my database it wipes out the standard admin#example.com login for Active Admin.
In my seed file i set a user as having role :admin but this login only works for the frontend of the app and not the backend active admin login. How can i remedy this? Thanks!
Note* Im using Devise + cancan + rolify
Seeds.rb
user2 = User.create! :name => 'Second User', :email => 'user2#example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => Time.now.utc
puts 'New user created: ' << user2.name
user.add_role :admin
Active Admin generally has another table for users known as admin_users. Please try the following in your console
user = AdminUser.create :email => 'user2#example.com', :password => 'please'
I am using faker to generate sample data. I have this as follows:
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "rails",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
#name = Faker::Name.name
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
The problem is that I have a couple of after_save callbacks that are not being called when the User is created. Why is that? Thanks
The methods:
after_save :create_profile
def create_profile
self.build_profile()
end
In all my reading, it seems that save! bypasses any custom before_save, on_save or after_save filters you have defined. The source code for create! reveals that it invokes save!. Unless you absolutely NEED the bang version, why are you using it? Try removing all your bang methods and just invoking the non-bang versions:
[1..100].each do |n|
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.new(:name => name, :email => email, :password => password, :password_confirmation => password)
if !user.save
puts "There was an error while making #{user.inspect}"
end
end
I have my Rails app setup with Devise, but it's still in the development stages. I also have a Thor task that creates the default Admin user.
Problem is Devise won't let that admin user login to the site until the account is confirmed. Is there any way I can disable the confirmable module for the creation of specific users, such as Admin?
My Thor task:
class Setup < Thor
desc "create_admin [EMAIL] [PASSWORD]", "Creates the administrative user."
def create_admin(email = "admin#bogus.com", password = "testpassword123")
require File.expand_path('config/environment.rb') # load Rails environment
admin = User.create(:email => email, :password => password, :password_confirmation => password)
admin.assign_role :admin
puts "Admin user (#{ email }) created."
end
end
Once your user is created you can call the devise provided confirm! method on it rather than updating the database directly. Eg.:
admin = User.create(:email => email, :password => password, :password_confirmation => password)
admin.assign_role :admin
admin.confirm!
This should work
admin = User.create(:email => email, :password => password, :password_confirmation => password)
So your confirmed_at is set, which is the field devise refers to when checking user confirmation.
EDIT
Forgive me if this seems like a hack but this seems to work for me. After executing the above line,
admin.update_attributes(:confirmed_at => Time.now)
just coment :confirmable in User or Admin model
devise :database_authenticatable, :recoverable, :rememberable, :trackable, #:confirmable...
In config/inicializers/devise.rb
you can set here how many time user have to confirm his account