Ruby on rails 在RubyonRails 4.2.0中隐藏记录而不是删除记录
我想隐藏对账单的一些记录,而不是完全删除它们,以便在临时隐藏后检索相应的余额 请参考屏幕截图以便更好地理解,如下所示 我将如何进行 index.html.erbRuby on rails 在RubyonRails 4.2.0中隐藏记录而不是删除记录,ruby-on-rails,Ruby On Rails,我想隐藏对账单的一些记录,而不是完全删除它们,以便在临时隐藏后检索相应的余额 请参考屏幕截图以便更好地理解,如下所示 我将如何进行 index.html.erb <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="table-responsive myTable"> <table id = "kola" clas
<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>
“,”,:精度=>2)%>
“,”,:精度=>2)%>
“,”,:精度=>2)%>
= 0 ? “pos”:“neg”%>
“,”,:精度=>2)%>
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
类Xvaziriclass 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
class CreateXvaziris
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
classxvaziriscontroller
欢迎提出任何建议
提前谢谢。您可以引入新的字段
隐藏
,默认情况下该字段为假
,因此将显示所有现有数据。然后,对于每个要隐藏的记录,将此字段(标志)设置为true
执行余额方法以包括或排除隐藏记录。完成此步骤
还有一个gem-act-as-paranoid,它也做同样的事情这里有一个添加隐藏字段的实用帮助: 通过在控制台中键入以下内容添加迁移:
rails g migration AddHiddenToXvaziris hidden:boolean
打开生成的迁移文件并更改以下行:
add_column :xvaziris, :hidden, :boolean, :default => false
保存文件并运行迁移:
rake db:migrate
然后,您必须检查hidden属性是否为false,并且仅当hidden为false时才向@xvaziris
添加一行。在xvaziris_controller.rb中,只需更改如下索引方法:
def index
@xvaziris = Xvaziri.find_by hidden: false
@xvaziris = @xvaziri.search(params[:search])
respond_to do |format|
format.js
format.html
end
end
调用销毁操作时,还必须将隐藏属性设置为true。
在xvaziris_controller.rb中更改销毁方法:
def destroy
@xvaziri = Xvaziri.find(params[:id])
@xvaziri.hidden = true
flash[:notice] = 'Xvaziri was successfully hidden.'
redirect_to xvaziris_url
end
注意,元素现在并没有被销毁,而是被设置为hidden==true。 如果您希望在其他地方销毁元素,我建议保留销毁方法,并在xvaziris_controller.rb中添加一个新方法:
def hide_xvaziri
@xvaziri = Xvaziri.find(params[:id])
@xvaziri.hidden = true
flash[:notice] = 'Xvaziri was successfully hidden.'
redirect_to xvaziris_url
end
进行此操作时,还必须在路线中添加以下行:
如果需要,您可以自定义路线,以满足您的需要
要再次显示隐藏的元素,我建议执行以下操作: 将“全部显示”按钮添加到index.html.erb: 在xvaziris_controller.rb中,使用以下方法:
def reset_filter
xvaziris = Xvaziri.all // or do the search here, depending on what you need
xvaziris.each do |xvaziri|
xvaziri.hidden = false
end
redirect_to xvaziris_url
end
编辑:此答案无效,因为它假定OP希望永久存档记录 使用范围
已存档
。我想你知道怎么做somexvaziri.archived=true
设置为标记不想再显示的记录可以添加布尔字段,单击“删除”后,只需将属性更新为true,并在循环中显示false Records感谢回复。我该如何实际操作?感谢回复。我是新手,请你详细解释一下好吗?我只是想不考虑这个问题
<%= link_to "Show all", resetfilter_xvaziri_path %>
get '/xvaziri/resetfilter', to: 'xvaziris#reset_filter'
def reset_filter
xvaziris = Xvaziri.all // or do the search here, depending on what you need
xvaziris.each do |xvaziri|
xvaziri.hidden = false
end
redirect_to xvaziris_url
end
class Xvaziri < ActiveRecord::Base
default_scope { where(:archived => false) }
end