覆盖DeviseController基类–Rails 4,Devise 3
我试图覆盖Devise方法set_flash_message
。 设计文档介绍了如何覆盖各个子模块的控制器 。
但是,这个特殊的方法位于DeviseController
,它是所有模块的父类。
文档(包括wiki和inline)都没有说明如何实现这一点,所以我不确定如何最好地继续。 我相信最好的方法是简单地重新打开类并根据需要修改方法,然后在/lib
放置一个文件。 然而,似乎在Devise之前加载,导致错误喷出。
NameError in Devise::RegistrationsController#new undefined local variable or method `require_no_authentication' for #
DeviseController
的复杂父定义也可能具有净负面影响:
class DeviseController < Devise.parent_controller.constantize
思考?
我相信这是覆盖Devise控制器的语法:
class RegistrationsController < Devise::RegistrationsController
如果你收到方法错误,你需要记住这不会完全覆盖控制器 - 你的方法将被委托给“main”设计控制器,所以你可以使用这样的东西:
def method super your_code_here end
更新
class SessionsController < DeviseController prepend_before_filter :require_no_authentication, :only => [ :new, :create ] prepend_before_filter :allow_params_authentication!, :only => :create prepend_before_filter { request.env["devise.skip_timeout"] = true } prepend_view_path 'app/views/devise' # GET /resource/sign_in def new self.resource = resource_class.new(sign_in_params) clean_up_passwords(resource) respond_with(resource, serialize_options(resource)) end # POST /resource/sign_in def create self.resource = warden.authenticate!(auth_options) set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_to do |format| format.json { render :json => {}, :status => :ok } format.html { respond_with resource, :location => after_sign_in_path_for(resource) } end end # DELETE /resource/sign_out def destroy redirect_path = after_sign_out_path_for(resource_name) signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)) set_flash_message :notice, :signed_out if signed_out && is_navigational_format? # We actually need to hardcode this as Rails default responder doesn't # support returning empty response on GET request respond_to do |format| format.all { head :no_content } format.any(*navigational_formats) { redirect_to redirect_path } end end protected def sign_in_params devise_parameter_sanitizer.sanitize(:sign_in) end def serialize_options(resource) methods = resource_class.authentication_keys.dup methods = methods.keys if methods.is_a?(Hash) methods << :password if resource.respond_to?(:password) { :methods => methods, :only => [:password] } end def auth_options { :scope => resource_name, :recall => "#{controller_path}#new" } end end
Devise.parent_controller
在devise/devise.rb
中的Devise模块定义中定义。 幸运的是,它声明了mattr_accessor
,因此您可以自己设置值(默认值为“ApplicationController”)。 在应用程序初始化过程中执行此操作可能是最有意义的,例如,与initializers/devise.rb
的其余Devise配置initializers/devise.rb
。
我做了文件:
config/initilializers/devise_controller.rb
并把:
DeviseController.class_eval do protected def set_flash_message .... end def resource_params .... end end
这似乎可以解决问题。 我担心加载顺序,如果该类尚不存在,至少class_eval会给你一个错误,而不是默默地覆盖它。