【问题标题】:Populate or display entries from bottom to top of the table row of the expenses table in ruby on rails在 ruby​​ on rails 中从下到上填充或显示费用表的表行的条目
【发布时间】:2021-02-10 17:24:29
【问题描述】:

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

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 %>

expenses_controller.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"),但无法获得真正的余额。

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

欢迎提出任何建议。

提前谢谢你。

【问题讨论】:

    标签: ruby ruby-on-rails-6.1


    【解决方案1】:

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

    <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 %>
    

    【讨论】:

    • 感谢您的回复。它不起作用,因为每次迭代余额都变为零并且不会结转。
    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多