Redirect_to并在Rails中的同一个动作中返回两次?

时间:2013-04-08 13:09:33

标签: ruby-on-rails redirect

我有以下代码:

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,但我仍然遇到同样的错误。

1 个答案:

答案 0 :(得分: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