我正在尝试重写设计方法set_flash_message
。Devise文档涵盖了如何override controllers for the various submodules。
但是,这个特定的方法位于DeviseController
中,它是所有模块的父类。
文档( wiki和内联)没有说明如何实现这一点,所以我不确定如何才是最好的。我认为最好的方法是简单地重新打开类并根据需要修改方法,为此我在/lib
中放置了一个文件。然而,它似乎是在设备之前加载,导致错误喷发。
NameError in Devise::RegistrationsController#new
undefined local variable or method `require_no_authentication' for #<Devise::RegistrationsController>
DeviseController
的复杂父定义也可能产生净负面影响:
class DeviseController < Devise.parent_controller.constantize
有什么想法?
发布于 2014-02-14 18:21:00
我相信这是覆盖Devise控制器的语法:
class RegistrationsController < Devise::RegistrationsController
如果你收到方法错误,你需要记住这不会完全覆盖控制器-你的方法将被委托给“主”设备控制器,所以你可以这样使用:
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
发布于 2016-06-13 21:31:12
在devise/devise.rb
的Devise模块定义中定义了Devise.parent_controller
。Luckily, it has mattr_accessor
declared,因此您可以自己设置该值(默认值为"ApplicationController")。在应用程序初始化过程中执行此操作可能是最有意义的,例如,与initializers/devise.rb
中的其余设备配置一起执行。
发布于 2018-06-06 05:05:31
我做了这个文件:
config/initilializers/devise_controller.rb
然后放入:
DeviseController.class_eval do
protected
def set_flash_message
....
end
def resource_params
....
end
end
这似乎起到了作用。我关心的是加载顺序,如果类不存在,至少class_eval会给你一个错误,而不是默默地覆盖它。
https://stackoverflow.com/questions/21769980
复制相似问题