2017-03-13 37 views
1

我需要什麼,如果一些用戶有一個屬性,這將不需要確認。 我見過一些關於此的帖子,但我無法理解它(我在Rails中有點新手)。設計 - 有可能某些用戶不需要確認嗎?

在我user.rb

def confirmation_required? 
    if self.name == 'Joe' 
     false 
    end 
    end 

我試過了,但沒有任何反應,就像是永遠是假的。我看到另一篇與此代碼:

def confirmation_required? 
     !confirmed? 
    end 
#Put your conditions and job's done ! 

,但我怎麼能訪問用戶數據從我user.rb(模型)注意,用戶來自HTTP POST請求。

有人可以幫我嗎?

感謝

編輯

而且我可能只是重新寫設計:: RegistrationsController到這樣的事情:

class RegistrationsController < Devise::RegistrationsController 

def create 
    super do 
    if resource.name == 'Joe' 
      resource.skip_confirmation! 
      resource.save 
    end 
    end 
    end 
end 

你認爲這能解決嗎? 謝謝。

回答

1

在您的用戶模型可以有條件地skip_confirmation!before_save回調

class User < ActiveRecord::Base 
    before_save :skip_confirm # arbitrary method name 

    def skip_confirm 
    if self.name == 'Joe' 
     skip_confirmation! 
    end 
    end 

end 

,或者你可以在before_save

class User < ActiveRecord::Base 

    before_save -> do 
    if self.name == 'Joe' 
     skip_confirmation! 
    end 
    end 

end 
+0

這麼簡單使用塊!男人非常感謝你! –

相關問題