Reputation: 31
This is a really weird error for me, and I've already scoured Google but cannot find anyone with a similar problem. Any help or suggestions would be appreciated!
Started a new Ruby/Rails project recently, using the same stack as a previous, recent, working project. I can access the current_user
and its family of helpers from every class that extends ApplicationController
, but cannot access any such method from ApplicationController < ActionController::Base
, wherein I try to alias that method under another name (see below). The error given is also below.
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>'
$ rvm reload
)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'
Upvotes: 1
Views: 1207
Reputation: 2918
The current_user
method appears dynamically in ApplicationController
. Check it out
So during the initializing ApplicationController class ruby can't find this method. Honestly the initializing process is more complicated. Devise defines current_user
helper when routes are loaded but application controller is loaded before. So I see here 2 solutions:
Add this line to ApplicationController
before aliasing the current_user
method:
Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {}))
Define current_user
method in ApplicationController
using default implementation:
def current_user
@current_user ||= (warden.authenticate(scope: :user) || GuestUser.new)
end
I personally prefer 2nd variant because the 1st solution loads devise twice. I think this can break something in your controllers. But if you have tests for your current_user
method the 2nd solution solves the problem completely even on devise updates.
Upvotes: 1
Reputation: 7225
try this out for aliasing the current_user method.
alias :old_current_user, :current_user
Upvotes: 0