2017-05-03 19 views
0

我寫了一個動作,它再次調用另一個私有方法,如果滿足特定條件,我想重定向到其他頁面或繼續執行。 但我無法做到, 當我試圖使用redirect_to root_path它說雙渲染, 它實際上是試圖執行實際調用的行動聲明,而不是從私有方法渲染。如何在redirect_to被調用後停止執行

def actual_method_called 
    data1 = params[:data1] 
    method_2(data1) 
    data2 = params[:data1] 
    method_2(data2) 
    render json: {status: 'ok' } 
end 

private 

def method_2(data) 
    if data.valid? 
    puts 'continue the execution' 
    else 
    redirect_to root_path and return 
    end 
end 
+0

您使用on Rails的Ruby的是什麼版本? –

+0

Rails 5.0.0.1版本 ruby​​ 2.2.5p319 – chinna2580

+0

您可以將'method_2'放入'before_action'過濾器中。 –

回答

0

您可以從調用方法返回一個值...

def method_2(data) 
    if data.valid? 
    puts 'continue the execution' 
    return 
    else 
    redirect_to root_path 
    return :redirected 
    end 
end 

聞你怎麼稱呼它,你存儲返回值

def actual_method_called 
    data1 = params[:data1] 
    return_status ||= method_2(data1) 
    data2 = params[:data1] 
    return_status ||= method_2(data2) 
    render json: {status: 'ok' } unless return_status == :redirected 
end