21
votes

I am setting up devise and need to be able to use devise helpers. When I add before_filter :authenticate_user! into the application controller I get the following error undefined method 'authenticate_user!' for #<HomeController:*>

It is not present in my home controller directly, but inherited from the application controller, if I use skip_before_filter :authenticate_user! in my home (or any controller) and try to access the page I get the following error.

undefined method `user_signed_in?' for #<#<Class *>

It breaks on the line that in the layouts/application.html.erb file that calls it. The same things happens for any of the helpers. It looks as if they are not being loaded at all, however, when I loaded up the rails console, and entered $LOAD_PATH.dup the output included

 "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/controllers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/helpers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/mailers" 

So it seems that they should indeed be loaded by rails.

This is my application controller

class ApplicationController < ActionController::Base

helper :all # include all helpers, all the time
protect_from_forgery
include ActionView::Helpers::NumberHelper
before_filter :authenticate_user!

end

I did restart the server after installing devise, and multiple times as I tried different attempts to solve the problem. If any more files or information is needed please let me know. Thank you very much in advance.

Edit: My user.rb file was requested to show that I have indeed installed devise

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :encryptable, :encryptor => :restful_authentication_sha1  

  # Setup accessible (or protected) attributes for your model  
  attr_accessible :email, :password, :password_confirmation, :remember_me  

end

Edit: The entire error message

NoMethodError in HomeController#index

undefined method `authenticate_user!' for #< HomeController:0x000000040c45a0 > '

Here is the full trace

activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `run_2094683035699451847__process_action_2229546931289158113_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in >`_run_process_action_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'

activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'

actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'

actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'

actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'

journey (1.0.4) lib/journey/router.rb:68:in `block in call'

journey (1.0.4) lib/journey/router.rb:56:in `each'

journey (1.0.4) lib/journey/router.rb:56:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'

warden (1.2.1) lib/warden/manager.rb:35:in `block in call'

warden (1.2.1) lib/warden/manager.rb:34:in `catch'

warden (1.2.1) lib/warden/manager.rb:34:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'

rack (1.4.5) lib/rack/etag.rb:23:in `call'

rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'

rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'

rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'

activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'

activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `run_177144612565476380_call_4534346825225857812__callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'

railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'

activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'

rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'

rack (1.4.5) lib/rack/runtime.rb:17:in `call'

activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'

rack (1.4.5) lib/rack/lock.rb:15:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'

railties (3.2.13) lib/rails/engine.rb:479:in `call'

railties (3.2.13) lib/rails/application.rb:223:in `call'

railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'

passenger (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'

passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in `accept_and_process_next_request'

passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in `main_loop'

passenger (4.0.2) lib/phusion_passenger/request_handler.rb:449:in `block (4 levels) in start_threads'

passenger (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in `disable_interruptions'

passenger (4.0.2) lib/phusion_passenger/request_handler.rb:444:in `block (3 levels) in start_threads'

Edit: This is the the text from running Devise.helpers in the console.

$ rails console

Loading development environment (Rails 3.2.13)

1.9.3-p429 :001 > Devise.helpers

=> #< Set: {Devise::Controllers::Helpers} >

Edit: This my routes rb file. :Application.routes.draw do

devise_for :users  

resource :sessions, :only => [:new, :create, :destroy]

devise_scope :user do
    match 'signup' => 'users#new', :as => :signup
    match 'register' => 'users#create', :as => :register
    match '/login' => 'sessions#new', :as => :login 
     match 'logout' => 'sessions#destroy', :as => :logout
end
match '/activate/:activation_code' => 'users#activate', :as => :activate, :activation_code => nil

match '/users/:id', :to => 'users#show', :as => :user

resources :users do

member do
     put :suspend
     put :unsuspend
     delete :purge
end

end

resource :sessions

   other resources

match '' => 'home#index', :as => :home
match ':controller(/:action(/:id))'
root to: 'home#index'
end 
6
@Btuman, try experimenting with devise_for in routes.rb perhaps there's an error in the routes setup / namespacing issue. Try rake routes to see if they are setup correctly. I've had an issue like that before and it directly related to bad routes setupBogdan Rybak
@RickCigarette, why would helpers be affected by a bad route?Btuman

6 Answers

34
votes

Be sure to add devise_for :user in your routes.rb

See more here: https://stackoverflow.com/a/11180576

6
votes

Just restart your rails server.

"Note that you should re-start your app here if you've already started it. Otherwise you'll run into strange errors like users being unable to login and the route helpers being undefined." [Getting Started Instructions]

[edit] More detailed answer:

Please verify two additional things: Your HomeController should inherit from Devise::Controllers::Helpers. And the devise directive in route.rb (that generates the missing method in Helpers) should have extended Devise.@@mappings. The following code can check this:

class ApplicationController ...
  ...
  before_filter do
    fail "bad ancestor" unless self.kind_of?(Devise::Controllers::Helpers)
    fail "no mapping" unless Devise.class_variable_get(:@@mappings).[:user]
    authenticate_user!
  end
end

To introduce a similar problem in a working rails application the config/initializers/devise.rb and the config/routes.rb devise entries have to be disabled and rails has to be restarted. Reenabling the code will not fix the problem unless rails is restarted.

2
votes

There are many things which can go wrong. Some things to check in order:

  • Not being able to call before_filter :authenticate_user! indicates that devise is not correctly set up.
  • Add devise_for :users in routes.rb (its is users, you got this right), next simplify those scoped rules for testing.
  • You need to have the before_filter :authenticate_user! working to get the helpers in the controller or include the helpers manually
  • If your base controller does not inherit from ActionController then you need to add the helper methods manually: Put this in your base controller helper_method "current_user", "user_signed_in?", "user_session"
  • One thing to check: Does self.controller.current_user work in your erb? This would indicate the helper_methods not being included.
2
votes

I added this line to my controller which did the trick for me:

include Devise::Controllers::Helpers
0
votes

I had the same problem (Rails 5.1.2, Devise 4.3.0) and it happened to be that I was using :-

devise_for :user do
  ...
end

instead of

devise_for :users do
  ...
end

I fixed it by changing the namespace.

user => users

0
votes

I had the same problem following Devise's getting started guide.

Turns out I had mistakenly run rails generate model User rather than rails generate devise User, which just created a plain ActiveRecord model that happened to have the name 'User'.