我已经与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'
答案 0 :(得分:3)
Pundit的authorize
方法无意称为before_action
。 before_action
调用不带参数的方法,authorize
获取模型对象和可选的权限名称。这就是为什么你得到错误的原因。
你没有向我展示足够的相关代码,看看你接下来应该做什么,但我建议让Pundit在你的控制器操作中工作,然后再将其拉回到回调中。
这里有一个正确使用authorize
的示例:https://github.com/elabs/pundit
答案 1 :(得分:2)
您可以在控制器中定义私有帮助器方法(如果您将在所有控制器中使用reactiveValues
),则调用application_controller
,然后调用私有方法{{1} }。像这样:
authorize current_user
然后将其称为before_action
:
private
def perform_authorization
authorize current_user
end