I just started a new Rails 3.2.8 app, and keep receiving this error. It occurs anytime I directly hit a Devise controller, or use the authenticate_user! filter.
Started GET "/users/sign_in" for 127.0.0.1 at 2012-09-17 22:55:00 -0500
Processing by Devise::SessionsController#new as HTML
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `[]' for nil:NilClass):
warden (1.2.1) lib/warden/session_serializer.rb:31:in `fetch'
warden (1.2.1) lib/warden/proxy.rb:212:in `user'
warden (1.2.1) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.1) lib/warden/proxy.rb:104:in `authenticate'
warden (1.2.1) lib/warden/proxy.rb:114:in `authenticate?'
/Users/nicholaswyoung/.rvm/gems/ruby-1.9.3-p194-dev/bundler/gems/devise-8c7bf7dd15af/app/controllers/devise_controller.rb:124:in `require_no_authentication'
activesupport (3.2.8) lib/active_support/callbacks.rb:418:in `_run__225892201280898280__process_action__4512450677408931391__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/mapper.rb:42: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.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
mongoid (3.0.6) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
mongoid (3.0.6) lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
mongoid (3.0.6) lib/rack/mongoid/middleware/identity_map.rb:33: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'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__93877302595973219__call__1631561807570460414__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/nicholaswyoung/.rvm/rubies/ruby-1.9.3-p194-dev/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/nicholaswyoung/.rvm/rubies/ruby-1.9.3-p194-dev/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/nicholaswyoung/.rvm/rubies/ruby-1.9.3-p194-dev/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /Users/nicholaswyoung/.rvm/gems/ruby-1.9.3-p194-dev/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/nicholaswyoung/.rvm/gems/ruby-1.9.3-p194-dev/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
Rendered /Users/nicholaswyoung/.rvm/gems/ruby-1.9.3-p194-dev/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.2ms)
@nicholaswyoung : why did you close this issue ? Have you found a solution ?
I did. To be honest, I'm not sure what I did wrong to cause the error, but it was definitely my fault, and not Devise.
Honestly, I was probably far too tired from working late last night. That's what likely caused the bug. ;)
I started a new app again, slowly walked through the install process, and everything worked as expected.
I found the issue. I was planning to build a portion of my app using the rails-api project. When included in your Gemfile, it apparently removes a critical dependency.
I'll leave it up to you to determine whether this is a valid bug, and the ticket should be reopened or not.
@nicholaswyoung : If you can prove that rails-api can make some issues with Devise, could you be kind and edit the wiki please ? (until a patch would be write).
I allow myself to ping @josevalim if Nicholas should edit th wiki. ^^
I just ran into this exact same problem. What is the necessary dependency that gets removed with rails-api?
Heres the fix -
In config/application.rb after "class Application < Rails::Application" add the following 2 lines:
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
That helped!
Adding those two lines solved the problem. How did you find out?
If those two middleware are required for warden to serialize to session, perhaps a dependency should be added to warden for them. Thoughts @josevalim?
Hi, i'm facing the same issue but I don't use Devise. I'm using directly Warden. The problem happens systematically when I try to instantiate a Warden::Proxy object. Typically, at each request. Did someone figured out what's wrong ?
edit : I think that the nil value stands for session hash. (https://github.com/hassox/warden/blob/master/lib/warden/session_serializer.rb#L31)
I had the same issue with https://github.com/rails-api/rails-api and devise.
@Leonas's solution fixes this.
I also had to do this: https://coderwall.com/p/uhbj0a
But could not get it working, I'm looking at using warden straight up for my rails-api needs.
Shouldn't Devise provide a "sessionless" approach?
@xjunior Devise already provides a session-less approach, TokenAuthenticatable.
@latortuga Kind of. In the end it does set the session because Warden does that. When it succeed to authenticate it passes that to warden through success!(model), which saves the current user in the session.
It only uses the session if you haven't excluded your strategy from session storage. See lib/devise.rb and look for skip_session_storage. Token auth by default has it enabled to use session storage because one of the use
cases is to login with a token. You can just as well disable session storage if you don't want to support that use case.
On May 27, 2013 8:23 PM, "Carlos Júnior" [email protected] wrote:
@latortuga https://github.com/latortuga Kind of. In the end it does set
the session because Warden does that. When it succeed to authenticate it
passes that to warden through success!(model), which saves the current user
in the session.—
Reply to this email directly or view it on GitHubhttps://github.com/plataformatec/devise/issues/2065#issuecomment-18520154
.
@latortuga Not quite… unfortunately if you don't enable session (cookie or anything else) your app will blow up.
In rails-api, if you don't add this:
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
You're gonna get this:
NoMethodError (undefined method `[]' for nil:NilClass):
warden (1.2.1) lib/warden/session_serializer.rb:31:in `fetch'
warden (1.2.1) lib/warden/proxy.rb:212:in `user'
warden (1.2.1) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.1) lib/warden/proxy.rb:127:in `authenticate!'
/Users/cp/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/bundler/gems/devise-10c9a492ab27/lib/devise/controllers/helpers.rb:48:in `authenticate_account!'
As soon as you try to access your current_user or, as in this case, authenticate the account.
This is more of a warden issue, but seem like we could workaround this in Devise.
In the above example I used token_authentication, and it's set to not use session.
This also solves the problem:
class NullSessionStore < ActionDispatch::Session::AbstractStore
def initialize(app, options = {})
super
end
def get_session(env, sid)
sid ||= generate_sid
[sid, {}]
end
def set_session(env, sid, session, options)
sid
end
def destroy_session(env, sid, options)
generate_sid
end
end
config.middleware.use NullSessionStore
@xjunior You're absolutely right, I reproduced this as a warden issue and am submitting a PR to fix it.
I can still reproduce this error with "destroy" method, the culprit is probably https://github.com/hassox/warden/blob/master/lib/warden/proxy.rb#L234
Started DELETE "/users/logout.json" for 10.0.2.2 at 2013-07-09 21:41:32 +0000
Processing by Users::SessionsController#destroy as JSONset.rb:655:in `call'
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."authentication_token" = 'vUMNwthNFKayv1eqgxzd' LIMIT 1
(0.3ms) BEGINrvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:34:in `catch'
(0.3ms) COMMITvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:34:in `call'
Completed 500 Internal Server Error in 77ms'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
NoMethodError (undefined method `[]' for nil:NilClass):
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/proxy.rb:234:in `session'
devise (3.0.0.rc) lib/devise/hooks/timeoutable.rb:11:in `block in <top (required)>'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/hooks.rb:14:in `call'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/hooks.rb:14:in `block in _run_callbacks'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/hooks.rb:9:in `each'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/hooks.rb:9:in `_run_callbacks'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:53:in `_run_callbacks'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/proxy.rb:179:in `set_user'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/proxy.rb:323:in `_perform_authentication'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/proxy.rb:127:in `authenticate!'
devise (3.0.0.rc) lib/devise/controllers/helpers.rb:48:in `authenticate_user!'all'
activesupport (4.0.0) lib/active_support/callbacks.rb:377:in `_run__829292324425542535__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'ed'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process''
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'ver.rb:138:in `service'
actionpack (4.0.0) lib/action_dispatch/routing/mapper.rb:44:in `call'server.rb:94:in `run'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call':in `block in start_thread'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'b/action_dispatch/middleware/templates/rescues/missing_tem
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:35:in `block in call'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:34:in `catch'
/home/vagrant/.rvm/gems/ruby-2.0.0-p195/bundler/gems/warden-9f55b43c2545/lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1267672954020761935__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Rendered /home/vagrant/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb
(0.5ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.3ms)
I'm getting this same undefined method '[]' for nil::NilClass when trying to use the Rally service hook in GitHub Enterprise 2.1.2, /data/hookshot/current/vendor/gems/ruby/2.1.0/gems/github-services-1.0.0.05205ac/lib/services/rally.rb:119
any way to solve this problem?
@ecauli see my comkment from May 29, 2013
Most helpful comment
Heres the fix -
In config/application.rb after "class Application < Rails::Application" add the following 2 lines:
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore