覆盖DeviseController基类 - Rails 4,Devise 3 [英] Override DeviseController base class - Rails 4, Devise 3

查看:150
本文介绍了覆盖DeviseController基类 - Rails 4,Devise 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖Devise方法 set_flash_message 。 Devise文档涵盖了如何覆盖各种子模块的控制器

I am trying to override the Devise method set_flash_message. Devise documentation covers how to override controllers for the various submodules.

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

However this particular method is located within DeviseController, the parent class of all the modules.

文档(维基和内联)都不了解如何实现此目的,所以我不知道如何最好地进行。我相信最好的方法是简单地重新打开类并根据需要修改方法,并且我将一个文件放在 / lib 中。但是,似乎在Devise之前加载,导致错误发生。

The documentation (both wiki and inline) says nothing about how to achieve this, so I'm not sure how best to proceed. I believe the best approach would be to simply re-open the class and modify the method as needed, and I placed a file in /lib to that effect. However it appears that is getting loaded prior to Devise, resulting in error spew.

NameError in Devise::RegistrationsController#new
undefined local variable or method `require_no_authentication' for #<Devise::RegistrationsController>

DeviseController 的复杂父级定义也可以具有净负面影响:

The complex parent definition for DeviseController may also be having an net negative effect:

class DeviseController < Devise.parent_controller.constantize

想法?

推荐答案

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

I believe this is the syntax to override Devise controllers:

class RegistrationsController <  Devise::RegistrationsController

如果你收到方法错误,你需要记住这不会完全覆盖控制器 - 您的方法将被从主设计控制器委派,所以你可以使用这样的东西:

If you're receiving method errors, you need to remember this won't totally overwrite the controller - your methods will be delegated to from the "main" devise controller, so you can use things like this:

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

这篇关于覆盖DeviseController基类 - Rails 4,Devise 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆