2013-04-08 39 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

相關問題