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

时间:2014-02-14 02:54:03

标签: ruby-on-rails ruby devise

我试图覆盖Devise方法set_flash_message。设计文档介绍了如何override controllers for the various submodules

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

文档(wiki和inline)都没有说明如何实现这一点,所以我不确定如何最好地继续。我认为最好的方法是简单地重新打开类并根据需要修改方法,然后我在/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

思想?

4 个答案:

答案 0 :(得分:0)

我相信这是覆盖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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我制作了文件:

config/initilializers/devise_controller.rb

并提出:

DeviseController.class_eval do
  protected

  def set_flash_message
    ....
  end

  def resource_params
    ....
  end
end

这似乎可以解决问题。我担心加载顺序,如果类已经不存在,至少class_eval会给你一个错误,而不是默默地覆盖它。

答案 3 :(得分:0)

我只需要在一个特定的控制器(find_message)中覆盖set_flash_messagesRegistrationsController

要覆盖/附加到该方法,我最终只是在RegistrationsController本身中编写了该方法。可以将其提取到模块中并在多个Devise Controllers中使用,从而为修改后的方法提供单一位置。

这样,我也可以使用super。我在将superclass_eval一起使用时遇到了一些困难...

我的控制器现在看起来像这样:

class RegistrationsController < Devise::RegistrationsController

  ###

  protected 

  def find_message(kind, options = {})
    # my custom code here
    super
  end
end