Ruby on rails 在操作中多次调用渲染和/或重定向

Ruby on rails 在操作中多次调用渲染和/或重定向,ruby-on-rails,devise,authorization,pundit,Ruby On Rails,Devise,Authorization,Pundit,我用的是设计和专家。 要创建新的配置文件页面,必须授权用户这样做。 自从我第一次实现它以来,它一直运行良好,但今天它刚开始出现错误消息: 在此操作中多次调用渲染和/或重定向。 请注意,您最多只能调用render或redirect 每个动作一次。还要注意,重定向和渲染都不会终止 操作的执行,因此如果要在 重定向,您需要执行类似“重定向到(…)和 返回” 以下是我的应用程序控制器的代码: rescue_from Pundit::NotAuthorizedError, with: :user_

我用的是设计和专家。 要创建新的配置文件页面,必须授权用户这样做。 自从我第一次实现它以来,它一直运行良好,但今天它刚开始出现错误消息:

在此操作中多次调用渲染和/或重定向。 请注意,您最多只能调用render或redirect 每个动作一次。还要注意,重定向和渲染都不会终止 操作的执行,因此如果要在 重定向,您需要执行类似“重定向到(…)和 返回”

以下是我的应用程序控制器的代码:

    rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
    ...

    private

    def user_not_authorized(exception)
        flash[:alert] = "Sorry, you are not authorized to perform this action."
        redirect_to(request.referrer || root_path)
    end
def new
  @profile_page = ProfilePage.new
  authorize @profile_page
end

def create
  @profile_page = ProfilePage.new(profile_page_params)
    respond_to do |format|
      if @profile_page.save
        format.html { redirect_to @profile_page, notice: 'Profile page was successfully created.' }
        format.json { render :show, status: :created, location: @profile_page }
      else
        format.html { render :new }
        format.json { render json: @profile_page.errors, status: :unprocessable_entity }
      end
    end
    authorize @profile_page
  end
这是我的ProfilePage控制器:

    rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
    ...

    private

    def user_not_authorized(exception)
        flash[:alert] = "Sorry, you are not authorized to perform this action."
        redirect_to(request.referrer || root_path)
    end
def new
  @profile_page = ProfilePage.new
  authorize @profile_page
end

def create
  @profile_page = ProfilePage.new(profile_page_params)
    respond_to do |format|
      if @profile_page.save
        format.html { redirect_to @profile_page, notice: 'Profile page was successfully created.' }
        format.json { render :show, status: :created, location: @profile_page }
      else
        format.html { render :new }
        format.json { render json: @profile_page.errors, status: :unprocessable_entity }
      end
    end
    authorize @profile_page
  end
有人建议我在下面添加这行代码
flash[:alert]

self.response_body = nil

但是现在,我的用户再次被重定向到“新建配置文件”页面,而不是成功的配置文件页面。它还告诉用户,他们无权完成此操作,尽管它已授权他们完成此操作。

在创建操作中,您必须在保存记录之前放置授权逻辑:

你得走了

authorize @profile_page
在创建操作的顶部,在初始化
@profile\u页面之后,如下所示:

def create
  @profile_page = ProfilePage.new(profile_page_params)
    authorize @profile_page

    respond_to do |format|
      if @profile_page.save
        format.html { redirect_to @profile_page, notice: 'Profile page was successfully created.' }
        format.json { render :show, status: :created, location: @profile_page }
      else
        format.html { render :new }
        format.json { render json: @profile_page.errors, status: :unprocessable_entity }
      end
    end
end

如果每个
操作都有多个
渲染
/
重定向到
的错误消息
,则必须使用早期的
返回
,如
返回重定向到某个路径
。这里的问题在于您显示的代码中,我看不出有任何错误。我已经从ProfilePage控制器添加了代码。我应该在哪里提前返回?错误发生在哪里<代码>新建
或在创建actionWorks中创建
操作
!啊,我一定是在没有意识到的情况下移动了它,这就是它突然停止工作的原因。干杯