【问题标题】:Pundit::AuthorizationNotPerformedError attempting to adapt microposts to Devise/PunditPundit::AuthorizationNotPerformedError 试图使微博适应 Devise/Pundit
【发布时间】:2016-02-24 14:11:16
【问题描述】:

我是 Rails 的新手,我正在第二次阅读 Michael Hartl 的出色 Rails Tutorial,这一次我试图将第 11 章和第 12 章的微博改编为一​​个简单的 Devise/Pundit 应用程序。米工作。我可以通过种子文件创建微博并显示它们,但是当我实际尝试通过该站点创建新帖子时,我遇到了 Pundit 的授权错误。我得到的错误是:

Pundit::AuthorizationNotPerformedError in MicropostsController#create 

我的 Microposts 控制器如下所示:

class MicropostsController < ApplicationController 
  before_action :authenticate_user!
  after_action :verify_authorized

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to current_user
    else
      @feed_items = []
      flash[:danger] = "Unable to create micropost!"
    end
  end

  def destroy
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content)
    end
end

我认为没有为“创建”操作正确设置授权,但我不确定应该如何设置。我没有 Pundit for Microposts 的政策。我尝试添加一个简单的,但它没有改变任何东西。我正在学习将所有这些部分放在一起,有人能指出我正确的方向吗?

【问题讨论】:

    标签: ruby-on-rails-4 devise pundit


    【解决方案1】:

    有一个操作后过滤器 verify_authorized,因此您会收到此错误。如果您为创建操作创建了策略,则使用该策略来消除错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多