2011-08-08 50 views
0

我正在使用rails 3和創建支出維護應用程序。 在這個每個條目都有支出金額字段,我正在通過ajax成功添加/刪除/更新它。 但我試圖刷新總金額值,每當添加,刪除編輯操作完成。更新總數入門添加,刪除和更新ajax

看我的代碼:

刪除控制器功能:

def destroy 
    @expenditure = Expenditure.find(params[:id]) 
    @expenditure.destroy 

    **@total = Expenditure.where(:user_id => current_user.id).sum('amount')** 
    //@total calculated and to be updated in view 

    respond_to do |format| 
     format.html { redirect_to(@expenditure) } 
     format.js { head :ok } 
    end 
    end 

後的支出被刪除我這樣做:

$('.delete_post').live('ajax:success', function() { 
    $(this).parent().parent().fadeOut(); 
    **$("#totalBox").html("<%= escape_javascript(render(:partial => 'total' , :locals => { :total => expenditures.calculateTotal })).html_safe %>");** 
}); 

索引視圖:

<div class="blocks" id='**totalBox**'> 
    <%= render :partial => 'total' , :locals => { :total => @total }%> 
</div> 

總的部分:

Summary 
<hr/> 
<div class="actionItems"> 
    Total Expenditure <> <span id='totalexp'><%= total %></span> 
</div> 

在不久的將來,我必須要通過更多的值,以該部分像 上個月的費用, 朋友1費用, FRIEND2費用,

請幫我解決這個問題? 也建議一種方法來解決這種類型的問題? 我是一個新bi- 幫助// SOS

+0

你們是不是要刷新視圖,數據庫,或兩者兼而有之? – yoozer8

+0

問題更新;添加說明 – dbKooper

回答

0

您應該將此行代碼從常見js文件移至destroy.js,位於appropritate控制器目錄下的views目錄中。根據你的描述很可能這將是app/views/expenditure/destroy.js.erb

$("#totalBox").html("<%= escape_javascript(render(:partial => 'total' , :locals => { :total => @total })).html_safe %>"); 

同時刪除從format.jshead :ok只要把它作爲format.js

+0

感謝man..it是那麼簡單.... – dbKooper

+0

關鍵是'total'是動態數據,您需要保持它的動態性,並且不要將其放在只加載一次的公用文件夾下的常用js文件中。 – rubish