1

我有以下代码:

def edit
    @post = Post.find(params[:id])
    if !current_user.posts.include?(@post)
      permission_denied
    end
    respond_to do |format|
      format.html  # edit.html.erb
      format.json  { render :json => @post }
    end
end

 def permission_denied
    flash[:notice] = 'Sorry, you are not authorized to access that page.'
    redirect_to root_url
 end

如何调整此代码,因此它不会显示“在此操作中多次调用渲染和/或重定向”。我尝试将“并返回”添加到redirect_to root_url and return,但我不断收到相同的错误。

4

1 回答 1

1

您应该从编辑操作返回:

def edit
  @post = Post.find(params[:id])
  return permission_denied if !current_user.posts.include?(@post)

  respond_to do |format|
    format.html  # edit.html.erb
    format.json  { render :json => @post }
  end
end
于 2013-04-08T13:13:27.703 回答