1

これは私にとって非常に奇妙なエラーであり、Google を調べましたが、同様の問題を抱えている人を見つけることができませんでした。ヘルプや提案をいただければ幸いです。

最近、以前の最近の作業中のプロジェクトと同じスタックを使用して、新しい Ruby/Rails プロジェクトを開始しました。current_userを拡張するすべてのクラスから およびそのヘルパー ファミリにアクセスできますが、からそのApplicationControllerようなメソッドにアクセスすることはできませんApplicationController < ActionController::Base。そこで、そのメソッドを別の名前でエイリアスしようとします (以下を参照)。与えられたエラーは以下にもあります。

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  alias_method :old_current_user, :current_user
  helper_method :current_or_guest_user

  def current_or_guest_user
    if current_user
      if session[:guest_user_id]
        logging_in
        guest_user.destroy
        session[:guest_user_id] = nil
      end
      current_user
    else
      guest_user
    end
  end
end

スタックトレース

/Users/shaneckamar/Documents/Development/afraidtotrade/app/controllers/application_controller.rb:4:in `alias_method': undefined method `current_user' for class `ApplicationController' (NameError)
    from /Users/shaneckamar/Documents/Development/afraidtotrade/app/controllers/application_controller.rb:4:in `<class:ApplicationController>'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/app/controllers/application_controller.rb:1:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-2.2.4/app/controllers/devise_controller.rb:2:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-2.2.4/app/controllers/devise/sessions_controller.rb:1:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.6.0/lib/active_admin/devise.rb:57:in `<module:Devise>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.6.0/lib/active_admin/devise.rb:4:in `<module:ActiveAdmin>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.6.0/lib/active_admin/devise.rb:3:in `<top (required)>'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config/routes.rb:2:in `block in <top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `instance_exec'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `eval_block'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:267:in `draw'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config/routes.rb:1:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:108:in `reload_routes!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.6.0/lib/active_admin/reloader.rb:34:in `reload!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.6.0/lib/active_admin/reloader.rb:61:in `block in attach!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__1010026444251623978__prepare__3846619713470166991__callbacks'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config/environment.rb:5:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config.ru:3:in `block in <main>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config.ru:in `new'
    from /Users/shaneckamar/Documents/Development/afraidtotrade/config.ru:in `<main>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/server.rb:200:in `app'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/server.rb:46:in `app'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/server.rb:254:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/server.rb:70:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>' 

環境・プロジェクト内容

  • 古いプロジェクトと新しいプロジェクトはどちらも同じ gem を使用します。
  • Mac OS X 10.8を実行している
  • RVM、現在の ruby​​/gemset はruby​​-1.9.3-p392 です
  • レール 3.2.1

試みられた救済策

  • 新しい Gemfile のバージョンと古いプロジェクトの Gemfile.lock のバージョンを一致させることに細心の注意を払いました。
  • 再起動しました。数回。
  • リロードされた RVM (経由$ rvm reload)
  • POW/Anvilを再開しました
  • pow/anvil を使用する代わりに、コンソールから起動しようとしました
  • 古い作業中のプロジェクトと新しいプロジェクトを何時間も比較し、構成または初期化ファイルの違いを見つけようとしました。何もありませんでした。

Gemfile

gem 'rails', '3.2.13'
gem 'pg'


group :assets do


gem 'sass-rails',   '~> 3.2.3'
      gem 'coffee-rails', '~> 3.2.1'
      gem 'less-rails'
      gem 'bourbon'
      gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails',
          :github => 'anjlab/bootstrap-rails',
          :branch => '3.0.0'
      gem 'therubyracer', :platform => :ruby
      gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails', '2.2.1'

group :development do
      gem 'rails-erd'
      gem 'better_errors', '>= 0.2.0'
      gem 'binding_of_caller', '>= 0.6.8', :platform => :ruby
      gem 'pry-rails'
      gem 'pry-remote'
end

gem 'haml'
gem 'haml-rails'
gem 'activeadmin'
gem 'meta_search',    '>= 1.1.0.pre'
gem 'turbolinks'
gem 'devise', '2.2.4'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter'
gem 'oauth2'
gem 'paperclip', '~> 3.0'
gem 'jquery-turbolinks'
gem 'braintree'
gem 'simple_form'
4

2 に答える 2

1

メソッドはcurrent_userに動的に表示されApplicationControllerます。調べてみてください ApplicationController クラスの初期化中に ruby​​ はこのメソッドを見つけることができません。正直なところ、初期化プロセスはより複雑です。Devise はcurrent_user、ルートが読み込まれるときにヘルパーを定義しますが、アプリケーション コントローラーはその前に読み込まれます。だから私はここに2つの解決策を見ます:

  1. メソッド をApplicationControllerエイリアシングする前に、次の行を追加します。current_user

    Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {}))

  2. デフォルトの実装を使用してcurrent_userメソッドを定義します。ApplicationController

    def current_user @current_user ||= (warden.authenticate(scope: :user) || GuestUser.new) end

1 番目のソリューションはデバイスを 2 回ロードするため、個人的には 2 番目のバリアントを好みます。これにより、コントローラーの何かが壊れる可能性があると思います。ただし、メソッドのテストがある場合current_user、2番目のソリューションは、デバイスの更新でも問題を完全に解決します。

于 2014-04-29T00:14:49.910 に答える
0

current_user メソッドをエイリアシングするためにこれを試してください。

    alias :old_current_user, :current_user
于 2013-08-01T20:18:38.733 に答える