Ruby on rails 4 Rails 4-Pundit::PolicyScopingNotPerformedError
我是一名新的权威人士(我习惯于坎坎舞…),我正在接受这方面的培训,试图遵循自述。。但是,当从我的工作表模型中为所有工作表编制索引时,我遇到了PolicyScoping错误Ruby on rails 4 Rails 4-Pundit::PolicyScopingNotPerformedError,ruby-on-rails-4,pundit,Ruby On Rails 4,Pundit,我是一名新的权威人士(我习惯于坎坎舞…),我正在接受这方面的培训,试图遵循自述。。但是,当从我的工作表模型中为所有工作表编制索引时,我遇到了PolicyScoping错误 #application_controller.rb class ApplicationController < ActionController::Base include Pu ndit .../... end #sheets_controller.rb class SheetsContro
#application_controller.rb
class ApplicationController < ActionController::Base
include Pu ndit
.../...
end
#sheets_controller.rb
class SheetsController < ApplicationController
after_filter :verify_authorized, :except => :index, unless: :devise_controller?
after_filter :verify_policy_scoped, :only => :index
before_action :set_sheet, only: [:show, :edit, :update, :destroy]
respond_to :html, :json
def index
@sheets = SheetPolicy::Scope.new(current_user, Sheet).resolve
build_json_header_list
respond_to do |format|
format.html
format.json {
build_json_data_list
render json: @data
}
end
end
.../...
end
#sheet_policy.rb
class SheetPolicy < ApplicationPolicy
.../...
class Scope < Scope
def resolve
byebug
if user.system_admin?
scope.all
else
scope.where(:user_id => user.id)
end
end
end
end
但我认为(我一定是错了…)用工作表策略解决范围应该授权索引操作?权威文件对此不是很清楚
这是控制台日志
=====================
# development.log
Started GET "/sheets" for ::1 at 2015-09-18 11:59:30 +0200
Processing by SheetsController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
[18, 27] in .../app/policies/sheet_policy.rb
(byebug) user.system_admin?
Role Load (22.9ms) SELECT "roles".* FROM "roles"
true
(byebug) c
CACHE (0.3ms) SELECT "roles".* FROM "roles"
Rendered sheets/index.html.erb within layouts/administration (17.0ms)
.../...
Rendered layouts/administration/_scripts.html.erb (286.9ms)
Completed 500 Internal Server Error in 29650ms (Views: 6149.5ms | ActiveRecord: 25.3ms)
------
Pundit::PolicyScopingNotPerformedError - Pundit::PolicyScopingNotPerformedError:
pundit (1.0.1) lib/pundit.rb:107:in `verify_policy_scoped' activesupport (4.2.3)
lib/active_support/callbacks.rb:430:in `block in make_lambda'
.../...
activesupport (4.2.3) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (4.2.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
.../...
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `block in instrument'
.../...
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
.../...
activerecord (4.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
您不应该直接调用
SheetPolicy::Scope.resolve
方法-使用Pundit的助手方法
在这种情况下,您只需调用@sheets=policy\u scope(Sheet)
,这也是专家正在检查的内容
=====================
# development.log
Started GET "/sheets" for ::1 at 2015-09-18 11:59:30 +0200
Processing by SheetsController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
[18, 27] in .../app/policies/sheet_policy.rb
(byebug) user.system_admin?
Role Load (22.9ms) SELECT "roles".* FROM "roles"
true
(byebug) c
CACHE (0.3ms) SELECT "roles".* FROM "roles"
Rendered sheets/index.html.erb within layouts/administration (17.0ms)
.../...
Rendered layouts/administration/_scripts.html.erb (286.9ms)
Completed 500 Internal Server Error in 29650ms (Views: 6149.5ms | ActiveRecord: 25.3ms)
------
Pundit::PolicyScopingNotPerformedError - Pundit::PolicyScopingNotPerformedError:
pundit (1.0.1) lib/pundit.rb:107:in `verify_policy_scoped' activesupport (4.2.3)
lib/active_support/callbacks.rb:430:in `block in make_lambda'
.../...
activesupport (4.2.3) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (4.2.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
.../...
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `block in instrument'
.../...
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
.../...
activerecord (4.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'