我以前使用過Pundit Gem,但我從來沒有嘗試過現在正在嘗試做的事情,出於某種原因,Pundit並不開心。沒有在Rails中發現的專家策略
我打算做的是在'索引'(Foos)頁面上創建一個帶有'create'(Foo)表單的模式。因此我需要實例化一個空的Foo對象來使模態窗體工作。
我遇到的問題是,當我遠程提交表單時,Pundit會引發錯誤。錯誤是:
權威人士:: NotDefinedError - 無法找到的零
我試圖理解爲什麼發生這種情況,但我沒能解決這個問題還沒有政策。
這裏是我的foos_controller.rb#指數:
...
def index
@foo = Foo.new
authorize @foo, :new?
@foos = policy_scope(Foo)
end
...
然後我有我的其他動作即運行以下「before_action」過濾器「創造」
...
before_action :run_authorisation_check, except: [:index]
def run_authorisation_check
authorize @foo
end
...
的政策,我'm使用在foo_policy.rb中:
....
def index?
user.has_any_role? :super_admin
end
def create?
user.has_any_role? :super_admin
end
def new?
create?
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.has_any_role? :super_admin
scope.all
end
end
end
....
直到我提交形成。有沒有人熟悉權威人士請幫助指導我瞭解我做錯了什麼?
UPDATE
完全foos_controller.rb
class FoosController < ApplicationController
def index
@foo = Foo.new
authorize @foo, :create?
@foos = policy_scope(Foo)
end
def new
@foo = Foo.new
end
def create
@foo = Foo.new(foo_params)
respond_to do |format|
if @foo.save
flash[:notice] = I18n.t("foo.flash.created")
format.json { render json: @foo, status: :ok }
else
format.json { render json: @foo.errors, status: :unprocessable_entity }
end
end
end
private
before_action :run_authorisation_check, except: [:index]
def foo_params
params.fetch(:foo, {}).permit(:bar)
end
def run_authorisation_check
authorize @foo
end
end
看來你可能不設置你的'@ foo'的價值,在調用':run_authorisation_check'方法之前,能否顯示完整的控制器? – oreoluwa
@oreoluwa我已更新我的問題 – Herm