为什么before_action:authorize会出现“参数数量错误”的问题?

3

我已经在我的应用程序中使用Devise和Pundit进行授权设置。在我的一个控制器中,我有before_action :authorize

然后我有以下测试:

describe SomeController do
  before(:each) do
    login_user(FactoryGirl.create(:user, :user_type => :admin))
  end

  describe "GET index" do
    it "it retrieves the index" do
      something = FactoryGirl.create(:Something)
      get :index
      assigns(:something).should eq([something])
    end
  end
end

我收到了以下错误信息:

wrong number of arguments (0 for 1)

登录助手相对简单:

module ControllerMacros
  def login_user(user)
    if user.nil?
      user = FactoryGirl.create(:user)
    end

    @request.env["devise.mapping"] = Devise.mappings[:user]
    sign_in user
  end
end

更新: 堆栈跟踪:

Failure/Error: get :index
     ArgumentError:
       wrong number of arguments (0 for 1..2)
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/pundit-0.2.3/lib/pundit.rb:62:in `authorize'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:424:in `block in make_lambda'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:160:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:160:in `block in halting'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:149:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:229:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:229:in `block in halting'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:149:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `block in halting'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `block in halting'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:166:in `block in halting'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:86:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/callbacks.rb:86:in `run_callbacks'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/abstract_controller/callbacks.rb:19:in `process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/notifications.rb:159:in `block in instrument'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.1/lib/active_support/notifications.rb:159:in `instrument'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/abstract_controller/base.rb:136:in `process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionview-4.1.1/lib/action_view/rendering.rb:30:in `process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/test_case.rb:595:in `process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/test_case.rb:64:in `process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.2.4/lib/devise/test_helpers.rb:19:in `block in process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.2.4/lib/devise/test_helpers.rb:72:in `catch'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.2.4/lib/devise/test_helpers.rb:72:in `_catch_warden'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.2.4/lib/devise/test_helpers.rb:19:in `process'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.1.1/lib/action_controller/test_case.rb:495:in `get'
     # ./spec/controllers/SomeController_spec.rb:31:in `block (4 levels) in <top (required)>'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `instance_eval'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `block in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `call'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
     # /Users/Me/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

请告诉我们您展示的代码中出现了哪些错误。 - Dave Schweisguth
运行单元测试时出现错误。 - Dofs
在哪一行的哪个文件中(以及在您展示的代码中对应什么)? - Dave Schweisguth
抱歉没有理解到确切的问题 :) 它是在 get: index 行出现故障。 - Dofs
我猜测问题不在于 get 自身失败,请显示整个回溯追踪。 - Dave Schweisguth
抱歉,现在有堆栈跟踪。 - Dofs
2个回答

3
Pundit的authorize方法不适用于被称为before_action的方法。 before_action调用一个没有参数的方法,而authorize需要一个模型对象和一个可选的权限名称。 这就是你收到错误的原因。
你没有展示足够相关的代码让我看出下一步该做什么,但我建议在控制器操作中让Pundit正常工作,然后再将其提取到回调函数中。
这里有一个正确使用authorize的示例:https://github.com/elabs/pundit

你是对的,错误是因为我在使用 before_action。我一直想着 Devise,而没有使用 after_action :verify_authorized。非常感谢! - Dofs

3

您可以在控制器中定义一个私有的辅助方法(或在application_controller中定义,如果您要在所有控制器中使用它),该方法调用authorize current_user,然后调用私有方法before_action。像这样:

    private
    def perform_authorization
      authorize current_user
    end

然后将其作为一个before_action调用:

    before_action :perform_authorization

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接