이전에 Pundit Gem을 사용했지만, 지금 내가하려고하는 일을 한 번도 해보지 못했으며, 어떤 이유로 든 전문가가 행복하지 않습니다.Pundit 정책이 Rails에서 발견되지 않았습니다.
목표로하고 싶은 것은 내 '인덱스'(Foos) 페이지에서 '생성'(Foo) 양식을 사용하는 것입니다. 따라서 모달 폼이 작동하려면 빈 Foo 객체를 인스턴스화해야합니다.
내가 겪고있는 문제는, 양식을 원격으로 제출할 때 Pundit에서 오류가 발생한다는 것입니다. 오류 :
학자 :: NotDefinedError -이 일어나고 있지만 아직 해결하지 못하고 한 이유를 이해하려고 노력 전무
의 정책을 찾을 수 없습니다. 여기
내 foos_controller.rb 번호의 인덱스 :...
def index
@foo = Foo.new
authorize @foo, :new?
@foos = policy_scope(Foo)
end
...
그때
...
before_action :run_authorisation_check, except: [:index]
def run_authorisation_check
authorize @foo
end
...
정책을 '생성', 즉 내 다른 작업에 대한 실행 다음 'before_action'필터가 그 I 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
....
오류는 제출할 때까지 표시되지 않습니다. 형태. Pundit에 익숙한 사람이 내가 잘못하고있는 것을 이해하도록 안내해 줄 수 있습니까?
UPDATE
오류 unable to find policy of nil
을 받고있어 왜 그래, 당신이 @foo
의 값을 설정하지 않을
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
@ololuwa 내 질문을 업데이트했습니다 – Herm