4

次の初期化子があります。

app/config/initializers/store_location.rb

module StoreLocation

  def self.skip_store_location
    [
        Devise::SessionsController,
        Devise::RegistrationsController,
        Devise::PasswordsController
    ].each do |controller|
      controller.skip_before_filter :store_location
    end
  end

  self.skip_store_location
end

私の ApplicationController の関連部分:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :convert_legacy_cookies
  before_filter :store_location

  alias_method :devise_current_user, :current_user

  def current_user
    # do something
  end

  private
  def store_location
    # store location
  end

さらに、これは config/environments/development.rbにあります

Foo::Application.configure do
# normal rails stuff
config.to_prepare do
    StoreLocation.skip_store_location
  end
end

RSpec/Rails に self.skip_store_location を実行させると、次のエラーが発生します。

/foo/app/controllers/application_controller.rb:7:in `alias_method': undefined method `current_user' for class `ApplicationController' (NameError)

呼び出しを削除すると、すべてが正常に戻ります (期待どおりにフィルターが実行されることを除く)。どういうわけか依存関係の読み込みを台無しにしていると思いますか?

4

1 に答える 1

20

The problem is that you use alias_method before the method is defined in ApplicationController. To fix the problem, move the line

alias_method :devise_current_user, :current_user

below

def current_user
  # do something
end

It's a bit misleading that the error appears when running skip_store_location. I assume that happens because skip_store_location loads several controllers, and one of them is a subclass of ApplicationController.

于 2012-08-07T15:05:56.560 に答える