2013-04-08 97 views
0

我有以下代码:Redirect_to并在Rails中的相同动作中返回两次?

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

您应该从编辑行动回报:

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 
+0

感谢您的回答。这实际上提出了另一个疑问,这是我在这里问:http://stackoverflow.com/questions/15880341/respond-to-and-redirect-in-the-same-action-in-rails – 2013-04-08 13:28:23

+0

@HommerSmith,你欢迎。在你的其他问题中,你不必担心这一点。这是一个if/else,所以它是一个或另一个。从来没有。 – Mischa 2013-04-08 13:45:20

相关问题