Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我需要计算rails 4中成本表中每一行的总计_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby 我需要计算rails 4中成本表中每一行的总计

Ruby 我需要计算rails 4中成本表中每一行的总计,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,在我的成本表中,我创建了一个变量来计算费率乘以小时数,我可以成功地获得我的数字。但是,我需要计算表末尾每行的总数。我的一个字段是“所有行的总计”。这方面的最佳行动方案是什么? 有什么建议吗? 在我的costs/index.html.erb文件中,这是我目前拥有的代码 <tbody> <% @costs.each do |cost| %> <tr> <td><%= cost.mini_description %></t

在我的成本表中,我创建了一个变量来计算费率乘以小时数,我可以成功地获得我的数字。但是,我需要计算表末尾每行的总数。我的一个字段是“所有行的总计”。这方面的最佳行动方案是什么? 有什么建议吗? 在我的costs/index.html.erb文件中,这是我目前拥有的代码

<tbody>
<% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>
    <td><%= cost.total_of_all_rows %></td>
    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
 <% end %>
 </tbody>
 <thead>
 <tr>
  <th>Total of all rows</th>
</tr>
</thead>

所有行的总数

我已经修复了我的代码,但是我仍然没有得到想要的结果。我所有行的总和没有加上速率和数量之和。我的修订守则是:

  <tbody>
<% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>

    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
</tbody>
<thead>
<tr>
  <th>Total of all rows</th>
</tr>
</thead>
  <tr>
    <td><%= cost.total_of_all_rows %><%=Cost.sum(:total) %></td>
  </tr>
<% end %>
<tbody>

</tbody>
</table>

所有行的总数
我对这个表的迁移是

 class CreateCosts < ActiveRecord::Migration
 def change
 create_table :costs do |t|
  t.string :mini_description
  t.string :description
  t.string :quantity
  t.string :rate
  t.string  :total
  t.string  :total_of_all_rows
  t.references :job, index: true

  t.timestamps
 end
end
class CreateCosts
结束

1)不要在数据库中存储所有行的总和,只要它是可变的,并且必须用每个新条目更新它。 2) 从迁移中删除列

3) 在你看来:

<tbody>
  <% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>

    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
</tbody>
<thead>
<tr>
  <th>Total of all rows</th>
</tr>
</thead>
  <tr>
    <td><%= @costs.sum(:total) %></td>
  </tr>
<% end %>
<tbody>

所有行的总数
如果仍要存储该值,请执行以下操作:

class Cost < ActiveRecord::Base

  after_save :update_totals

  def update_totals
    Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
  end
end
类成本
1)不要在数据库中存储所有行的总和,只要它是可变的,您必须用每个新条目更新它。 2) 从迁移中删除列

3) 在你看来:

<tbody>
  <% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>

    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
</tbody>
<thead>
<tr>
  <th>Total of all rows</th>
</tr>
</thead>
  <tr>
    <td><%= @costs.sum(:total) %></td>
  </tr>
<% end %>
<tbody>

所有行的总数
如果仍要存储该值,请执行以下操作:

class Cost < ActiveRecord::Base

  after_save :update_totals

  def update_totals
    Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
  end
end
类成本
1)不要在数据库中存储所有行的总和,只要它是可变的,您必须用每个新条目更新它。 2) 从迁移中删除列

3) 在你看来:

<tbody>
  <% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>

    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
</tbody>
<thead>
<tr>
  <th>Total of all rows</th>
</tr>
</thead>
  <tr>
    <td><%= @costs.sum(:total) %></td>
  </tr>
<% end %>
<tbody>

所有行的总数
如果仍要存储该值,请执行以下操作:

class Cost < ActiveRecord::Base

  after_save :update_totals

  def update_totals
    Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
  end
end
类成本
1)不要在数据库中存储所有行的总和,只要它是可变的,您必须用每个新条目更新它。 2) 从迁移中删除列

3) 在你看来:

<tbody>
  <% @costs.each do |cost| %>
  <tr>

    <td><%= cost.mini_description %></td>
    <td><%= cost.description %></td>
    <td><%= cost.quantity %></td>
    <td><%= cost.rate %></td>
    <td><%= cost.total%><%=cost.cost_var%></td>

    <!--<td><%#= cost.job %></td>-->

    <td><%= link_to 'Show', cost %></td>
    <td><%= link_to 'Edit', edit_cost_path(cost) %></td>
    <td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
</tbody>
<thead>
<tr>
  <th>Total of all rows</th>
</tr>
</thead>
  <tr>
    <td><%= @costs.sum(:total) %></td>
  </tr>
<% end %>
<tbody>

所有行的总数
如果仍要存储该值,请执行以下操作:

class Cost < ActiveRecord::Base

  after_save :update_totals

  def update_totals
    Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
  end
end
类成本

您可以显示您当前拥有的任何代码和/或您尝试过的代码吗?@AaronSiciano我已经添加了当前拥有的代码。谢谢您为什么不在for each循环的范围之外声明一个变量,并用0值初始化它。然后在foreach循环内部和结束时按cost.total值递增它循环你会得到总成本。还是我误解了这个问题?@AaronSiciliano我会尝试让你知道谢谢。@costs来自哪里?你能展示你当前拥有的代码和/或你尝试过的代码吗?@AaronSiciliano我已经添加了我当前拥有的代码。谢谢你为什么不在t之外声明一个变量计算foreach循环的作用域,并将其初始化为0。然后在foreach循环中增加cost.total的值,到循环结束时,您将获得总成本。或者我误解了这个问题?@AaronSiciliano我会尝试让您知道谢谢。@costs来自何方?您可以显示任何您需要的代码吗当前有和/或您尝试了什么?@AaronSiciliano我已经添加了当前的代码。谢谢您为什么不在for each循环的范围外声明一个变量,并用0值初始化它。然后在foreach循环内按cost.total值递增,到循环结束时,您将得到总cost、 还是我误解了这个问题?@AaronSiciliano我会尝试让你知道谢谢。@costs来自哪里?你能展示你当前拥有的代码和/或你尝试过的代码吗?@AaronSiciliano我已经添加了我当前拥有的代码。谢谢你为什么不在for each循环和ini的范围之外声明一个变量使用0的值对其进行调整。然后在foreach循环中增加cost.total的值,到循环结束时,您将获得总成本。或者我误解了这个问题?@Aaronsiciano我会尝试让您知道谢谢。@costs从何而来?谢谢您将尝试使用它。我使用了此选项,但出现了错误:“这是因为您要对单个对象中的所有列进行求和。如果要对成本表中的所有列进行求和,则必须执行Cost.sum(:column\u name)。但是,如果您只想对所拥有的数组进行求和,则必须执行:@costs.sum(:Cost\u var)很抱歉,我还是rails新手,所以我现在所做的是:“我仍然没有实现任何目标。我的语法有问题吗?正如我告诉你的,你没有访问成本表,你只是从你的.each.访问成本元素。你需要做(C)ost使用大写字母CThank,您可以尝试一下,我使用了这个,我得到了错误:“这是因为您对单个对象中的所有列进行求和。您必须进行Cost.sum(:column