首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >覆盖DeviseController基类-- Rails 4,Devise 3

覆盖DeviseController基类-- Rails 4,Devise 3
EN

Stack Overflow用户
提问于 2014-02-14 10:54:03
回答 4查看 2K关注 0票数 7

我正在尝试重写设计方法set_flash_message。Devise文档涵盖了如何override controllers for the various submodules

但是,这个特定的方法位于DeviseController中,它是所有模块的类。

文档( wiki和内联)没有说明如何实现这一点,所以我不确定如何才是最好的。我认为最好的方法是简单地重新打开类并根据需要修改方法,为此我在/lib中放置了一个文件。然而,它似乎是在设备之前加载,导致错误喷发。

代码语言:javascript
复制
NameError in Devise::RegistrationsController#new
undefined local variable or method `require_no_authentication' for #<Devise::RegistrationsController>

DeviseController的复杂父定义也可能产生净负面影响:

代码语言:javascript
复制
class DeviseController < Devise.parent_controller.constantize

有什么想法?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-02-14 18:21:00

我相信这是覆盖Devise控制器的语法:

代码语言:javascript
复制
class RegistrationsController <  Devise::RegistrationsController

如果你收到方法错误,你需要记住这不会完全覆盖控制器-你的方法将被委托给“主”设备控制器,所以你可以这样使用:

代码语言:javascript
复制
def method
    super
    your_code_here
end

更新

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2016-06-13 21:31:12

devise/devise.rb的Devise模块定义中定义了Devise.parent_controllerLuckily, it has mattr_accessor declared,因此您可以自己设置该值(默认值为"ApplicationController")。在应用程序初始化过程中执行此操作可能是最有意义的,例如,与initializers/devise.rb中的其余设备配置一起执行。

票数 1
EN

Stack Overflow用户

发布于 2018-06-06 05:05:31

我做了这个文件:

代码语言:javascript
复制
config/initilializers/devise_controller.rb

然后放入:

代码语言:javascript
复制
DeviseController.class_eval do
  protected

  def set_flash_message
    ....
  end

  def resource_params
    ....
  end
end

这似乎起到了作用。我关心的是加载顺序,如果类不存在,至少class_eval会给你一个错误,而不是默默地覆盖它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21769980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档