2016-05-16 56 views
1

注册和编辑配置文件时,我需要调用我的用户模型中的方法来启用或禁用不同的验证集。有没有办法做到这一点,而不必完全重写Devise的注册和注册方法?我宁愿不这样做。在设计注册或注册之前在用户中调用方法

我使用Ruby 2.3.1,Rails 4.2和Devise 4.1。

回答

2

最简单的方法可能是子类化设计的控制器为设计是相当不错条块允许定制:

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"}