我试图覆盖Devise方法 set_flash_message
。 Devise文档介绍了如何override controllers for the various submodules。
但是,此特定方法位于 DeviseController
(所有模块的父类)中。
该文档(Wiki和内联)都没有说明如何实现此目标,因此我不确定如何最好地进行。我相信最好的方法是简单地重新打开该类并根据需要修改该方法,为此我在/lib
中放置了一个文件。但是,似乎是在Devise之前加载的,从而导致错误喷出。
NameError in Devise::RegistrationsController#new
undefined local variable or method `require_no_authentication' for #<Devise::RegistrationsController>
DeviseController
的复杂父级定义也可能产生净负面影响:class DeviseController < Devise.parent_controller.constantize
有什么想法吗?
最佳答案
我相信这是重写Devise Controller 的语法:
class RegistrationsController < Devise::RegistrationsController
如果您收到方法错误,则需要记住这不会完全覆盖 Controller -您的方法将从“主要”的devise Controller 委派给您,因此您可以使用以下方法:
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
关于ruby-on-rails - 覆盖DeviseController基类-Rails 4,Devise 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21769980/