ruby on rails - Why does before_action :authorize fail with 'wrong number of arguments'? -
i have set pundit devise authorization on application. in 1 of controllers, have before_action :authorize
.
i have following test:
describe somecontroller before(:each) login_user(factorygirl.create(:user, :user_type => :admin)) end describe "get index" "it retrieves index" = factorygirl.create(:something) :index assigns(:something).should eq([something]) end end end
i receive error:
wrong number of arguments (0 1)
the login helper straight forward:
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
update: stacktrace:
failure/error: :index argumenterror: wrong number of arguments (0 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'
pundit's authorize
method not intended called before_action
. before_action
calls method no arguments, , authorize
takes model object , optional permission name. that's why you're getting error are.
you're not showing enough of relevant code me see should next, suggest getting pundit work in controller action before pulling out callback.
there example of correct use of authorize
here: https://github.com/elabs/pundit
Comments
Post a Comment