1

这对我来说是一个非常奇怪的错误,我已经搜索了谷歌但找不到任何有类似问题的人。任何帮助或建议将不胜感激!

最近开始了一个新的 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
  • 将旧的工作项目与新项目比较了几个小时,试图找出配置或初始化文件中的差异。没有。

宝石文件

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 找不到这个方法。老实说,初始化过程更复杂。Devisecurrent_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

我个人更喜欢第二种变体,因为第一种解决方案加载了两次。我认为这可能会破坏您的控制器中的某些内容。但是,如果您对您的current_user方法进行了测试,那么即使在设计更新时,第二种解决方案也可以完全解决问题。

于 2014-04-29T00:14:49.910 回答
0

试试这个给 current_user 方法起别名。

    alias :old_current_user, :current_user
于 2013-08-01T20:18:38.733 回答