Rails 在对某些参数添加限制后,Pundit 无法仅在 Create 操作上找到 nil 策略
Rails Pundit unable to find policy of nil only on Create action after adding restrictions on some params
我修改了我的一个 Goal.rb 模型,只允许管理员编辑一个字段。此模型及其中的所有操作在此更新之前均有效。此更新也适用于编辑操作,但适用于创建操作 returns
Pundit::NotDefinedError in GoalsController#create
unable to find policy of nil
我希望这只是一个语法错误,我不明白为什么它不起作用,因为没有 model/policy 文件名已更改,只有参数和策略设置(适用于编辑操作)
我认为这可能是因为我在创建它时调用了@goal 上的策略,但如果不是这样,我还能如何检查那里的权限?
goals_controller.rb:
def create
@goal = Goal.new(permitted_attributes(@goal))
#stuff
authorize @goal
respond_to do |format|
if @goal.save
# format.json { render :show, status: :created, location: @goal }
else
# format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
def update
authorize @goal
update_params = permitted_attributes(@goal)
#
end
respond_to do |format|
if @goal.update(update_params)
# format.json { render :show, status: :ok, location: @goal }
else
# format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
我刚刚意识到,您可以在新 class 上调用权限。我已经读了三遍,但只有在发布问题后才点击。
我正在尝试检查 @goal 的允许参数,它尚不存在并且显示为 nil 策略,但是使用 Pundit,您可以 运行 检查 [=] 的允许参数21=],在这种情况下:
@goal = Goal.new(permitted_attributes(目标))
我修改了我的一个 Goal.rb 模型,只允许管理员编辑一个字段。此模型及其中的所有操作在此更新之前均有效。此更新也适用于编辑操作,但适用于创建操作 returns
Pundit::NotDefinedError in GoalsController#create
unable to find policy of nil
我希望这只是一个语法错误,我不明白为什么它不起作用,因为没有 model/policy 文件名已更改,只有参数和策略设置(适用于编辑操作)
我认为这可能是因为我在创建它时调用了@goal 上的策略,但如果不是这样,我还能如何检查那里的权限?
goals_controller.rb:
def create
@goal = Goal.new(permitted_attributes(@goal))
#stuff
authorize @goal
respond_to do |format|
if @goal.save
# format.json { render :show, status: :created, location: @goal }
else
# format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
def update
authorize @goal
update_params = permitted_attributes(@goal)
#
end
respond_to do |format|
if @goal.update(update_params)
# format.json { render :show, status: :ok, location: @goal }
else
# format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
我刚刚意识到,您可以在新 class 上调用权限。我已经读了三遍,但只有在发布问题后才点击。
我正在尝试检查 @goal 的允许参数,它尚不存在并且显示为 nil 策略,但是使用 Pundit,您可以 运行 检查 [=] 的允许参数21=],在这种情况下:
@goal = Goal.new(permitted_attributes(目标))