2013-05-03 67 views
8

我使用的設計(2.2.3),我試圖加載「編輯」的形式使用此jQuery的Ajax調用用戶:Rails 3.2.13/Devise 2.2.3:方法「authenticate_scope!」拋出錯誤:錯誤的參數數目(1 0)

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

但是當我刪除 「:力=> 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」意味着......當我離開它時爲什麼會出現錯誤? 我想這是一個壞主意,像這樣猴子補丁寶石代碼。但我還能做些什麼來避免錯誤?

感謝您的幫助!

回答

19

想通了:這個問題是我已經重寫的方法...

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

    # then hand over to Devise's method 
    super 
end 

...在ApplicationController中。但它必須是這樣的:

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

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

希望幫助別人,有一天,也許......

+1

我想我不得不說現在:謝謝,@TomDogg,你真聰明:-) – TomDogg 2013-05-03 17:55:54

+0

兩個小時後沒有了解任何東西(但深入研究設計),謝謝!我們有完全相同的問題。你值得所有的問題,通過回答評論! :) – 2013-08-20 10:29:17

+0

剛剛有同樣的問題,謝謝! – ABrowne 2014-02-12 21:19:47