【问题标题】:Rails 3.2.13 / Devise 2.2.3: Method "authenticate_scope!" throws error: wrong number of arguments (1 for 0)Rails 3.2.13 / 设计 2.2.3:方法“authenticate_scope!”抛出错误:参数数量错误(1 代表 0)
【发布时间】:2013-05-03 17:34:03
【问题描述】:

我使用 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]

...来自 gem 文件...

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 代码进行猴子补丁是个坏主意。但是我还能做些什么来避免错误呢?

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails devise argument-error


    【解决方案1】:

    想通了:问题是我重写了方法...

    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
    

    希望有一天能对某人有所帮助……

    【讨论】:

    • 我想我现在不得不说:谢谢,@TomDogg,你真的很聪明 :-)
    • 两个小时后什么都不懂(但深入研究设计),谢谢!我们遇到了完全相同的问题。您应该从问题中得到所有的好处,通过回答到评论! :)
    • SO(和 TomDogg)来救援 - 感谢您花时间发布您的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2012-07-20
    相关资源
    最近更新 更多