2016-03-03 73 views
0

我使用這個第三方控制器:導軌 - 攔截respond_with

class LibController 

     def update 
     # 29 lines of code 
     respond_with resource 
     end 

    end 

我想要做的比respond_with在結束其他的東西。但我不想將所有29行復制/粘貼到MyController.update。可惜我不能想出一個辦法來渲染或重定向其他地方:

class MyController < LibController 

    def update 
     super 
     redirect_to somewhere_else 
    end 

    end 

我得到了DoubleRenderError: Render and/or redirect were called multiple times in this action。我認爲這是因爲respond_with立即調用render。有沒有辦法阻止/阻止?

謝謝!

回答

0

我認爲你正在做兩次重定向。 嘗試刪除您的更新方法中的一個重定向。

查看下面的示例代碼,顯示使用respond_with時的等效響應。

def create 
    @user = User.new(params[:user]) 
    flash[:notice] = 'User was successfully created.' if @user.save 
    respond_with(@user) 
end 

這是完全一樣的:

def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
    if @user.save 
     flash[:notice] = 'User was successfully created.' 
     format.html { redirect_to(@user) } 
     format.xml { render xml: @user, status: :created, location: @user } 
    else 
     format.html { render action: "new" } 
     format.xml { render xml: @user.errors, status: :unprocessable_entity } 
    end 
    end 
end 
+0

感謝aldrien。我想知道如果我唯一的選擇是從第三方方法複製/粘貼整個方法,只是爲了替換最後一行。 – Matt

+0

我想你只需要在Controller方法中使用重定向,並嘗試刪除「class LibController」中的respond_with。對不起,我只是猜測可能的方式,因爲我沒有看到你的完整代碼。嘗試操縱它,重要的是隻使用一個重定向。 –