Rails 4 - Pundit::PolicyScopingNotPerformedError
Rails 4 - Pundit::PolicyScopingNotPerformedError
我是 Pundit 的新手(我习惯了 Cancan...),我正在对其进行培训,试图遵循自述文件..但是在索引所有 sheet 时我遇到了 PolicyScoping 错误来自我的 Sheet 模特
#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
索引方法 a 已正确执行,sheet_policy 范围已正确解析(在控制台中调试...并且视图开始呈现...所以我猜错误来自 after_filer在 sheets_controller
after_filter :verify_policy_scoped, :only => :index
但我认为(我一定是错的...)使用 sheet 政策解决范围应该已经授权索引操作?Pundit 文档对此不是很清楚...
这里是控制台日志
=====================
# 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" = 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_scope(Sheet)
即可,这也是 Pundit 正在检查的内容。
我是 Pundit 的新手(我习惯了 Cancan...),我正在对其进行培训,试图遵循自述文件..但是在索引所有 sheet 时我遇到了 PolicyScoping 错误来自我的 Sheet 模特
#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
索引方法 a 已正确执行,sheet_policy 范围已正确解析(在控制台中调试...并且视图开始呈现...所以我猜错误来自 after_filer在 sheets_controller
after_filter :verify_policy_scoped, :only => :index
但我认为(我一定是错的...)使用 sheet 政策解决范围应该已经授权索引操作?Pundit 文档对此不是很清楚...
这里是控制台日志
=====================
# 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" = 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_scope(Sheet)
即可,这也是 Pundit 正在检查的内容。