wrong number of arguments (given 3, expected 2): devise (4.3.0) lib/devise/models/authenticatable.rb:228:in `serialize_from_session'
I am using Rails 5.1.1
ArgumentError - wrong number of arguments (given 3, expected 2):
devise (4.3.0) lib/devise/models/authenticatable.rb:228:in serialize_from_session'
devise (4.3.0) lib/devise.rb:478:in block (2 levels) in configure_warden!'
warden (1.2.7) lib/warden/session_serializer.rb:35:in fetch'
warden (1.2.7) lib/warden/proxy.rb:213:in user'
warden (1.2.7) lib/warden/proxy.rb:323:in _perform_authentication'
warden (1.2.7) lib/warden/proxy.rb:105:in authenticate'
devise (4.3.0) lib/devise/controllers/helpers.rb:124:in current_subscriber'
devise (4.3.0) lib/devise/controllers/helpers.rb:120:in subscriber_signed_in?'
I saw another issue here, with same problem, Issue was that they had a cookie from newer version of devise, which was being used on older version of devise, this usually happens when you are upgrading a bunch of stuck including devise in one branch And than you get back to some other branch for something and you have this newer cookie in your browser. Simple solution is to clear cookies in browser.
Hi @raghavendra-prithvi, thanks for your report. I've just tested Devise 4.3 with a Rails 5.1 application and everything worked as expected. Can you provide as a sample application that reproduces the issue?
It happened when I have both rails3 and rails5 applications opened at the sametime on the browser. I was in a process of migrating from rails3 to 5. If you use different browsers or if you use incognito for any one of them. It will be good.And This looks like an issue only in the firefox
@tegon I think that I'm facing the same issue, when trying to migrate to rails 5 and devise 4.3. Here you have the PR where I'm doing the migration and here you have the installation notes if you wanna take a look, I'm trying to dig deep into the code to see why we are facing the error, also it fails every time I'm using current_user. Please let me know if you need anything else :+1: .
To be a little more specific about what test you could go here and run this one bundle exec cucumber features/admin/admin_delete_charity.feature:23 and you will get:

with byebug:

and digging deeper:

@tegon I think that I know where the error is, analyzing the different versions of devise, I upgraded one by one until I found a change on the serialization methods on devise. I was using verision 3.5.10, then I did the following two upgrades:
- 4.0.0.rc1 - February 01, 2016 (157 KB)
- 4.0.0.rc2 - March 09, 2016 (158 KB)
didn't break, but the third one:
- 4.0.0 - April 18, 2016 (159 KB)
it was breaking, so digging deep into the code we can see a change for the following method on lib/devise.rb:
- 4.0.0.rc2 - March 09, 2016 (158 KB)
531 # A method used internally to complete the setup of warden manager after routes are loaded.
532 # See lib/devise/rails/routes.rb - ActionDispatch::Routing::RouteSet#finalize_with_devise!
533 def self.configure_warden! #:nodoc:
534 @@warden_configured ||= begin
535 warden_config.failure_app = Devise::Delegator.new
536 warden_config.default_scope = Devise.default_scope
537 warden_config.intercept_401 = false
538
539 Devise.mappings.each_value do |mapping|
540 warden_config.scope_defaults mapping.name, strategies: mapping.strategies
541
542 warden_config.serialize_into_session(mapping.name) do |record|
543 mapping.to.serialize_into_session(record)
544 end
545
546 warden_config.serialize_from_session(mapping.name) do |key|
547 # Previous versions contained an additional entry at the beginning of
548 # key with the record's class name.
549 args = key[-2, 2]
550 mapping.to.serialize_from_session(*args)
551 end
552 end
553
554 @@warden_config_blocks.map { |block| block.call Devise.warden_config }
555 true
556 end
557 end
this change was added here https://github.com/plataformatec/devise/pull/2300
- 4.0.0 - April 18, 2016 (159 KB)
465 # A method used internally to complete the setup of warden manager after routes are loaded.
466 # See lib/devise/rails/routes.rb - ActionDispatch::Routing::RouteSet#finalize_with_devise!
467 def self.configure_warden! #:nodoc:
468 @@warden_configured ||= begin
469 warden_config.failure_app = Devise::Delegator.new
470 warden_config.default_scope = Devise.default_scope
471 warden_config.intercept_401 = false
472
473 Devise.mappings.each_value do |mapping|
474 warden_config.scope_defaults mapping.name, strategies: mapping.strategies
475
476 warden_config.serialize_into_session(mapping.name) do |record|
477 mapping.to.serialize_into_session(record)
478 end
479
480 warden_config.serialize_from_session(mapping.name) do |args|
481 mapping.to.serialize_from_session(*args)
482 end
483 end
484
485 @@warden_config_blocks.map { |block| block.call Devise.warden_config }
486 true
487 end
488 end
this change was added here https://github.com/plataformatec/devise/pull/4032
Both methods are calling lib/devise/models/authenticatable.rb:
207 def serialize_from_session(key, salt)
208 record = to_adapter.get(key)
209 record if record && record.authenticatable_salt == salt
210 end
@fphilipe I would like to ask you why *args is being used since we already know how many parameters the method needs to receive? I think that it would be clearer if just the parameters the method expect to receive are passed when calling it.
My problem was that I was using:
page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
and now with the latest upgrade we just need to:
page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user))
That fixed my problem, I hope it helps :+1:
This is my PR upgrading devise from 3.5.10 to 4.4
@nisevi the two pull requests I've introduced are #2300 and #4032. They're 3 years apart. The diffs should give enough insights. Sorry, don't recall the details.
@fphilipe it doesn't for me, I already saw them and you will find them on my comments, that is why I was asking, thank you anyways :+1:
Judging from the code, *args was introduced in the first PR as a necessity for the upgrade path. In the second PR it could have been changed to explicit args as the args aren't being manipulated anymore. It could be changed to explicitly pass all args.
Thanks for discovering this @nisevi! 🎉 I'm going to close this issue.
Thanks, everyone!
Since the issue still exists in 4.6.0, is there any kind of workaround if you're not using set_rack_session explicitly?
Still seeing this when upgrading from Devise 1.5.4 to 4.7.1.
@mehagar that seems a big bump :). There was some code to support the change between 2.2.4 and 4.0.x it seems, so it might be worth considering going from major to major one at a time? Otherwise we'd need some more information to help.
In case others come across this:
I came across the same issue. I found a (unused) gem called "meta_search" was causing the issue. Found via closely reading through the framework trade to look for anything off. Noticed it was calling that gem which didn't make sense. Removed the gem and it worked fine.