9

我使用 Devise (2.2.3) 并尝试使用此 jQuery ajax 调用为用户加载“编辑”表单:

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

这将调用此 before_filter...

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

...来自宝石文件...

devise/app/controllers/devise/registrations_controller.rb

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

最终调用的方法是:

# 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

我得到的错误是:

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
```

但是当我删除“:force => true”时,错误就消失了:

# 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

所以我想知道“:force => true”是什么意思......为什么当我把它留在原地时会出现错误?我想像这样对 gem 代码进行猴子补丁是个坏主意。但是我还能做些什么来避免错误呢?

谢谢你的帮助!

4

1 回答 1

24

想通了:问题是我已经覆盖了方法......

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

  # then hand over to Devise's method
  super
end

...在应用程序控制器中。但它必须看起来像这样:

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

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

希望有一天能对某人有所帮助,也许...

于 2013-05-03T17:54:38.157 回答