使用ajax更新条目添加、删除和更新的总计数

使用ajax更新条目添加、删除和更新的总计数,ajax,ruby-on-rails-3,Ajax,Ruby On Rails 3,我正在使用rails 3并创建一个支出维护应用程序。 在这里,每个条目都有支出金额字段,我正在通过ajax成功地添加/删除/更新它。 但无论何时完成任何添加、删除编辑操作,我都会尝试刷新总量值 看看我的代码: 删除控制器功能: def destroy @expenditure = Expenditure.find(params[:id]) @expenditure.destroy **@total = Expenditure.where(:user_id => c

我正在使用rails 3并创建一个支出维护应用程序。 在这里,每个条目都有支出金额字段,我正在通过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 %>");**
});
$('.delete_post').live('ajax:success',function(){
$(this.parent().parent().fadeOut();
**$(“#totalBox”).html(“'total',:locals=>{:total=>expansions.calculateTotal}”).html_safe%>”**
});
索引视图:

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

'total',:locals=>{:total=>@total}%>
全部或部分:

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

总支出
在不久的将来,我必须传递更多的值给这个部分 上个月的费用, 友谊1费用, 朋友2费用

请帮我解决这个问题? 还建议解决此类问题的方法? 我是一个新的bi
Help//SOS

您应该将这一行代码从common js文件移动到Distroy.js,该文件位于相应控制器目录下的views目录中。根据您的描述,很可能是
app/views/expense/destroy.js.erb

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

同时从
format.js
中删除
标题:ok
,只需将其保留为
format.js

是否尝试在视图、数据库或两者中刷新?问题已更新;description added密钥是
total
是动态数据,您需要保持动态,不要将其放入公用文件夹下的公共js文件中,该文件只加载一次。