上下文:我正在为我的应用创建书签功能,该功能似乎正在运行,然后出现Pundit错误Pundit::AuthorizationNotPerformedError in SavedHairstylesController#create
这很奇怪,因为我有一个)为我的书签模型生成了一个名为save_hairstyle_policy.rb的策略,在该策略中,我为每个方法都返回了“ true”。
b)在我的Saved_Hairstyles控制器中,我有一个CREATE和DESTROY方法,并且在编写的每个方法中,我都授权了@saved_hairstyle
c)在我看来,我已经提到过这样的策略:policy(Saved_Hairstyle).create?
有什么想法吗?
要遵循的代码:
Saved_hairstyle_policy.rb:
class SavedHairstylePolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.all
end
end
def create?
return true
end
def destroy?
return true
end
end
Saved_Hairstyle控制器:
class SavedHairstylesController < ApplicationController
def create
@hairstyle = Hairstyle.find(params[:hairstyle_id])
authorize @saved_hairstyle
(current user) & hairstyle
@saved_hairstyle = SavedHairstyle.new(user: current_user, hairstyle: @hairstyle)
if @saved_hairstyle.save
respond_to do |format|
format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle) }
format.js
end
else
respond_to do |format|
format.html { render 'hairstyles' }
format.js
end
end
end
def destroy
@saved_hairstyle = SavedHairstyle.find(params[:id])
authorize @saved_hairstyle
@saved_hairstyle.destroy
@hairstyle = @saved_hairstyle.hairstyle
respond_to do |format|
format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle)}
format.js
end
end
end
index.html查看文件:
<div class="bookmark">
<% saved_hairstyle = SavedHairstyle.find_by(user: current_user, hairstyle: hairstyle.id) %>
<% if saved_hairstyle && policy(Saved_Hairstyle).create? %>
<%= link_to saved_hairstyle_path(saved_hairstyle), method: :delete do %>
<i class="fas fa-plus-circle"></i>
<% end %>
<% elsif %>
<% !saved_hairstyle && policy(Saved_Hairstyle).delete? %>
<%= link_to hairstyle_saved_hairstyles_path(hairstyle), method: :post do %>
<i class="fas fa-plus"></i>
<% end %>
<% end %>
</div>