1
註冊和編輯配置文件時,我需要調用我的用戶模型中的方法來啓用或禁用不同的驗證集。有沒有辦法做到這一點,而不必完全重寫Devise的註冊和註冊方法?我寧願不這樣做。在設計註冊或註冊之前在用戶中調用方法
我使用Ruby 2.3.1,Rails 4.2和Devise 4.1。
註冊和編輯配置文件時,我需要調用我的用戶模型中的方法來啓用或禁用不同的驗證集。有沒有辦法做到這一點,而不必完全重寫Devise的註冊和註冊方法?我寧願不這樣做。在設計註冊或註冊之前在用戶中調用方法
我使用Ruby 2.3.1,Rails 4.2和Devise 4.1。
最簡單的方法可能是子類化設計的控制器爲設計是相當不錯條塊允許定製:
class User < ActiveRecord::Base
validates :some_attribute, unless: :trusted?
def trusted?
[email protected]
end
def trusted!
@trusted = true
end
end
class MyApp::RegistrationsController < Devise::RegistrationController
def build_resource(hash=nil)
self.resource = resource_class.new_with_session(hash || {}, session)
self.resource.trusted!
end
end
# config/routes.rb
devise_for :users, :controllers => {:registrations => "my_app::registrations"}