2012-12-11 41 views
11

我使用設計寶石,並在創建用戶時我想跳過配置!並跳過配置電子郵件例如:設計寶石跳過確認並立即通過電子郵件跳過確認

User.create(:first_name => "vidur", :last_name => "punj").confirm!.skip_confirmation! 

但它只跳過確認,並不會跳過發送確認電子郵件。任何人都可以給我一個跳過兩個的想法。

+0

的http:// www.rubydoc.info/github/plataformatec/devise/Devise/Models/Confirmable# – Abhinay

回答

23

您需要呼叫skip_confirmation!在保存記錄之前。

嘗試

user = User.new(:first_name => "blah") 
user.skip_confirmation! 
user.save 
+0

這將跳過只發送確認電子郵件,但不確認用戶通過登錄表單登錄。 –

+2

根據設計代碼.. skip_confirmation設置confirmed_at屬性...同樣的事情確認!確實。見:https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb#L116 – Jamsi

+0

是的,你是對的.. –

4

skip_confirmation之前要調用User.create!你需要調用User.new後來user.save。

嘗試

user = User.new(:first_name => "vidur", :last_name => "punj") 
    user.skip_confirmation! 
    user.save! 
+0

很棒!感謝你的分享。 –

2

如果您不需要確認可言,你可以刪除模型中的:confirmable符號。

+2

我不能那樣做,因爲我需要網站其餘部分的確認程序。但是隻想爲創建用戶的特殊情況跳過它。 –

3
Got the solution: 
@user=User.new(:first_name => "vidur", :last_name => "punj") 
@user.skip_confirmation! 
@user.confirm! 
@user.save 
4

設置confirmed_at字段

User.create!(confirmed_at: Time.now, email: '[email protected]', ...) 

有用在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: '[email protected]') 
+0

這就是我需要的 - 謝謝! – Bijan

3

如果困惑在哪裏寫入skip_confirmation!方法在控制器還沒有生成設計控制器還然後:

在您的中寫下模型

before_create :my_method 

def my_method 
self.skip_confirmation! 
end 

現在只需使用:

user = User.new(:first_name => "Gagan") 
user.save 
+1

這確實有效!但每次調用save時都會觸發,其中包括對數據庫的「插入」和「更新」。我如何才能將它應用於「插入」而不是「更新」? – Yang

+2

@Yang您可以使用「before_create:my_method」回調代替before_save。 – bkunzi01

1

after_create: :skip_confirmation_notification - 檢查here

如果你想只在一定條件下skip_confirmation_notification,使用PROC