我正在使用 devise gem,在创建用户时我想跳过确认!并跳过确认电子邮件,例如:
User.create(:first_name => "vidur", :last_name => "punj").confirm!.skip_confirmation!
但它仅跳过确认,并且不跳过发送确认电子邮件。任何人都可以给我一个想法来跳过两者吗?
您需要拨打skip_confirmation!在保存记录之前。
尝试
user = User.new(:first_name => "blah")
user.skip_confirmation!
user.save
您在skip_confirmation之前调用User.create!,您需要稍后调用User.new和user.save。
尝试
user = User.new(:first_name => "vidur", :last_name => "punj")
user.skip_confirmation!
user.save!
设置confirmed_at字段
User.create!(confirmed_at: Time.now, email: '[email protected]', ...)
对种子很有用。rb
User.create_with(name: "Mr T", company: "Comp X", password: 'rubyrubyruby', password_confirmation: 'rubyrubyruby', confirmed_at: Time.now).find_or_create_by!( email: '[email protected]')
Got the solution:
@user=User.new(:first_name => "vidur", :last_name => "punj")
@user.skip_confirmation!
@user.confirm!
@user.save
如果您困惑在控制器中的何处编写
skip_confirmation!
方法,因为您还没有生成设计控制器:
将其写入您的
User
模型
before_create :my_method
def my_method
self.skip_confirmation!
end
现在只需使用:
user = User.new(:first_name => "Gagan")
user.save
如果您根本不需要确认,您可以删除模型中的
:confirmable
符号。