【发布时间】:2016-07-11 07:25:45
【问题描述】:
我想隐藏一些对账单的记录,而不是完全删除它们,以便在临时隐藏后检索相应的余额。
请参考以下截图以便更好地理解;
我将如何进行?
index.html.erb
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="table-responsive myTable">
<table id = "kola" class="table listing text-center">
<tr class="tr-head">
<td>Date</td>
<td>Description</td>
<td>Amount</td>
<td>Discount</td>
<td>Paid</td>
<td>Balance</td>
</tr>
<tr>
<td></td>
</tr>
<a href="#" class="toggle-formed" style="float: right;" >Search</a>
<div id="sample">
<%= form_tag xvaziris_path, remote: true, method: :get, class: "form-group", role: "search" do %>
<p>
<center><%= text_field_tag :search, params[:search], placeholder: "Search for.....", autofocus: true, class: "form-control-search" %>
<%= submit_tag "Search", name: nil, class: "btn btn-md btn-primary" %></center>
</p>
<% end %><br>
</div>
<%= render @xvaziris %>
</table>
</div>
</div>
</div>
_xvaziri.html.erb
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-1"><%= xvaziri.date.strftime('%d/%m/%Y') %></td>
<td class="col-3"><%= span_with_possibly_red_color xvaziri.description %></td>
<td class="col-1"><%= number_with_precision(xvaziri.amount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.discount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.paid, :delimiter => ",", :precision => 2) %></td>
<% @balance += xvaziri.amount.to_f - xvaziri.discount.to_f - xvaziri.paid.to_f %>
<% color = @balance >= 0 ? "pos" : "neg" %>
<td class="col-1 <%= color %>"><%= number_with_precision(@balance.abs, :delimiter => ",", :precision => 2) %></td>
</tr>
xvaziri.rb
class Xvaziri < ActiveRecord::Base
def to_s
description
end
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Xvaziri.create! row.to_hash
end
end
def self.search(search)
where (["description LIKE ? OR amount LIKE ? OR paid LIKE ?", "%#{search}%","%#{search}%","%#{search}%"])
end
end
20151128091020_create_xvaziris.rb
class CreateXvaziris < ActiveRecord::Migration
def change
create_table :xvaziris do |t|
t.date :date
t.text :description
t.decimal :amount
t.decimal :discount
t.decimal :paid
t.decimal :balance
t.timestamps null: false
end
end
end
xvaziris_controller.rb
class XvazirisController < ApplicationController
before_action :set_xvaziri, only: [:show, :edit, :update, :destroy]
layout "fedena"
def index
@xvaziris = Xvaziri.search(params[:search])
respond_to do |format|
format.js
format.html
end
end
def import
Xvaziri.import(params[:file])
redirect_to xvaziris_url, notice: "Xvaziris imported."
end
def show
end
def new
@xvaziri = Xvaziri.new
end
def create
@xvaziri = Xvaziri.new(xvaziri)
if
@xvaziri.save
flash[:notice] = 'Xvaziri Created'
redirect_to @xvaziri
else
render 'new'
end
end
def edit
end
def update
if @xvaziri.update(xvaziri)
flash[:notice] = 'Xvaziri Updated'
redirect_to @xvaziri
else
render 'edit'
end
end
def destroy
@xvaziri.destroy
flash[:notice] = 'Xvaziri was successfully destroyed.'
redirect_to xvaziris_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_xvaziri
@xvaziri = Xvaziri.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def xvaziri
params.require(:xvaziri).permit(:date, :description, :amount, :discount, :paid)
end
end
欢迎提出任何建议。
提前谢谢你。
【问题讨论】:
-
可以添加布尔字段,点击删除后更新属性为真,在你的循环中只显示假记录
-
感谢您的回复。我该怎么做呢?
标签: ruby-on-rails