Rails 3.2.13 / Devise 2.2.3: Method "authenticate_scope!" throws error: wrong number of arguments (1 for 0)
Asked Answered
S

1

10

I use Devise (2.2.3) and am trying to load the "edit" form for a user using this jQuery ajax call:

$.ajax({
  type: 'GET',
  url: '/users/edit',
  data: {
    id: id
  }
});

This will call this before_filter...

prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

...from the gem file...

devise/app/controllers/devise/registrations_controller.rb

(see: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb)

The method that's eventually called is:

# Authenticates the current scope and gets the current resource from the session.
def authenticate_scope!
  send(:"authenticate_#{resource_name}!"), :force => true)
  self.resource = send(:"current_#{resource_name}")
end

And the error I get is:

ArgumentError at /users/edit
============================

> wrong number of arguments (1 for 0)

(gem) devise-2.2.3/app/controllers/devise/registrations_controller.rb, line 116
-------------------------------------------------------------------------------

``` ruby
  111       signed_in_root_path(resource)
  112     end
  113   
  114     # Authenticates the current scope and gets the current resource from the session.
  115     def authenticate_scope!
> 116       send(:"authenticate_#{resource_name}!", :force => true)
  117       self.resource = send(:"current_#{resource_name}")
  118     end
  119   end
```

But when I delete the ":force => true", then the error vanishes:

# Authenticates the current scope and gets the current resource from the session.
def authenticate_scope!
  send(:"authenticate_#{resource_name}!"))  # deleted: :force => true
  self.resource = send(:"current_#{resource_name}")
end

So I'm wondering what the ":force => true" means... Why do I get the error when I leave it in place? I suppose it's a bad idea to monkey-patch gem code like this. But what else can I do to avoid the error?

Thanks for your help!

Seedman answered 3/5, 2013 at 17:34 Comment(0)
S
25

Figured it out: the problem was I had overriden the method...

def authenticate_user!
  # do some stuff
  # of my own here

  # then hand over to Devise's method
  super
end

...in the ApplicationController. But it must look like this:

def authenticate_user!(options={})
  # do some stuff
  # of my own here

  # then hand over to Devise's method
  super(options)
end

Hope that helps somebody, some day, maybe...

Seedman answered 3/5, 2013 at 17:54 Comment(4)
I guess I have to say now: Thanks, @TomDogg, you're really smart :-)Seedman
After two hours not understanding anything (but digging deeping into devise), thanks! We had the very exact same issue. You deserve all the uppoints from question, through answer to comment! :)Cumulate
SO (and TomDogg) to the rescue - thanks for taking the time to post your solutionLongo
2024 thanks man. You saved me.Jeraldinejeralee

© 2022 - 2024 — McMap. All rights reserved.