0

我只想在表格的第一行显示余额。

图片

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Expenses</h1>

<% balance = 0 %>

<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>

<div class="container">

    <table id="table_id" style="width:100%">
      <thead>
        <tr>
          <th>Date</th>
          <th>Particulars</th>
          <th>Debit</th>
          <th>Credit</th>
          <th>Balance</th>
        </tr>
      </thead>
    
      <tbody>
        <% @expenses.each do |expense| %>
          <tr>
            <td><%= expense.date.strftime('%d/%m/%Y') %></td>
            <td><%= expense.particulars %></td>
            <td class="pos"><%= expense.debit %></td>
            <td class="neg"><%= expense.credit %></td>
            <% balance += expense.debit.to_f-expense.credit.to_f %>
                    <% color = balance >= 0 ? "pos" : "neg" %>
                    <td class="<%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 0) %></td>        
                    <!--<td><%= link_to 'Show', expense %></td>-->
        <!--    <td><%= link_to 'Edit', edit_expense_path(expense) %></td>-->
        <!--    <td><%= link_to 'Destroy', expense, method: :delete, data: { confirm: 'Are you sure?' } %></td>-->
          </tr>
        <% end %>
      </tbody>
    </table>
</div>
    

<br>

<%= link_to 'New Expense', new_expense_path %>

费用_控制器.rb

class ExpensesController < ApplicationController
  before_action :set_expense, only: %i[ show edit update destroy ]

  # GET /expenses or /expenses.json
  def index
    @expenses = Expense.all
    # @expenses = Expense.order("created_at ASC")
    # @expenses = Expense.order("created_at DESC")
  end

  # GET /expenses/1 or /expenses/1.json
  def show
  end

  # GET /expenses/new
  def new
    @expense = Expense.new
  end

  # GET /expenses/1/edit
  def edit
  end

  # POST /expenses or /expenses.json
  def create
    @expense = Expense.new(expense_params)

    respond_to do |format|
      if @expense.save
        format.html { redirect_to @expense, notice: "Expense was successfully created." }
        format.json { render :show, status: :created, location: @expense }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @expense.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /expenses/1 or /expenses/1.json
  def update
    respond_to do |format|
      if @expense.update(expense_params)
        format.html { redirect_to @expense, notice: "Expense was successfully updated." }
        format.json { render :show, status: :ok, location: @expense }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @expense.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /expenses/1 or /expenses/1.json
  def destroy
    @expense.destroy
    respond_to do |format|
      format.html { redirect_to expenses_url, notice: "Expense was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_expense
      @expense = Expense.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def expense_params
      params.require(:expense).permit(:date, :particulars, :debit, :credit)
    end
end

我尝试在我的费用控制器中使用@expenses = Expense.order("created_at ASC")@expenses = Expense.order("created_at DESC"),但无法获得真正的平衡。

实际上,我必须使用适当的余额条目从下到上显示填充的条目。

任何建议都非常受欢迎。

先感谢您。

4

1 回答 1

0

如果我理解正确,您应该将balance变量的定义移到eachin 中index.html.erb

<p id="notice"><%= notice %></p>
    
    <h1>Expenses</h1>
        
    <style>
    table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
    }
    </style>
    
    <div class="container">
    
        <table id="table_id" style="width:100%">
          <thead>
            <tr>
              <th>Date</th>
              <th>Particulars</th>
              <th>Debit</th>
              <th>Credit</th>
              <th>Balance</th>
            </tr>
          </thead>
        
          <tbody>
            <% @expenses.each do |expense| %>
              <tr>
                <td><%= expense.date.strftime('%d/%m/%Y') %></td>
                <td><%= expense.particulars %></td>
                <td class="pos"><%= expense.debit %></td>
                <td class="neg"><%= expense.credit %></td>
                <% balance = 0 %>
                <% balance += expense.debit.to_f-expense.credit.to_f %>
                        <% color = balance >= 0 ? "pos" : "neg" %>
                        <td class="<%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 0) %></td>        
                        <!--<td><%= link_to 'Show', expense %></td>-->
            <!--    <td><%= link_to 'Edit', edit_expense_path(expense) %></td>-->
            <!--    <td><%= link_to 'Destroy', expense, method: :delete, data: { confirm: 'Are you sure?' } %></td>-->
              </tr>
            <% end %>
          </tbody>
        </table>
    </div>
        
    
    <br>
    
    <%= link_to 'New Expense', new_expense_path %>
于 2021-02-10T22:45:45.777 回答