如果Spree::Admin::ResourceController
,我想完全删除相应的行。
找到截图以便更好地理解,如下所示;
index.html.erb
tquantity = 0
purchases_controller.rb
<div class="col-md-10 col-md-offset-1">
<div class="table-responsive myTable">
<table class="table listing text-center">
<tr class="tr-head">
<td>DESCRIPTION</td>
<td>COLOR</td>
<td>QUANTITY</td>
<td>RETAIL PRICE</td>
<td>TOTAL AMOUNT</td>
<td>CARTON NO</td>
<td>CUSTOMER 1</td>
<td>CUSTOMER 2</td>
<td>ACTUAL QUANTITY</td>
</tr>
<% @purchases.each do |purchase| %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-2"><%= purchase.description %></td>
<td class="col-1"><%= purchase.color %></td>
<td class="col-2"><%= purchase.quantity %></td>
<td class="col-2"><%= number_with_precision(purchase.rprice, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= number_with_precision(purchase.tamount, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= purchase.cartonno %></td>
<td class="col-2"><%= purchase.cus1 %></td>
<td class="col-2"><%= purchase.cus2 %></td>
<td class="col-2"><%= tquantity = purchase.quantity - purchase.cus1 - purchase.cus2 %></td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
purchase.rb
class PurchasesController < ApplicationController
before_action :set_purchase, only: [:show, :edit, :update, :destroy]
# GET /Stockings
# GET /deldetails.json
def index
#@purchases = Purchase.all
@purchases = Purchase.where("tquantity !=?", 0)
end
def import
Purchase.import(params[:file])
redirect_to purchases_url, notice: "Purchases imported."
end
# GET /purchases/1
# GET /purchases/1.json
def show
end
# GET /purchases/new
def new
@purchase = Purchase.new
end
# GET /purchases/1/edit
def edit
end
# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(purchase_params)
respond_to do |format|
if @purchase.save
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
format.json { render :show, status: :created, location: @purchase }
else
format.html { render :new }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
if @purchase.update(purchase_params)
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: @purchase }
else
format.html { render :edit }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_purchase
@purchase = Purchase.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def purchase_params
params.require(:purchase).permit(:season, :category, :articleno, :description, :color, :quantity, :rprice, :tamount, :cartonno, :cus1, :cus2, :tquantity )
end
end
我是否需要使用SQL WHERE子句,如果是,那么我将如何获得所需的结果?
答案 0 :(得分:0)
在update
操作中,您需要检查新值是否会导致购买与您设置的删除条件相匹配。将这些更改用于update
操作以实现此目的:
# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
updated = @purchase.update(purchase_params)
deleted = (@purchase.quantity - @purchase.cus1 - @purchase.cus2) <= 0
if deleted
@purchase.destroy
format.html { redirect_to purchases_url, notice: 'Purchase was deleted.' }
format.json { head :no_content }
elsif updated
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: @purchase }
else
format.html { render :edit }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
关于视图,如果在数据中碰巧有任何应该过滤的行,您可以使用此查询:
# GET /Stockings
# GET /deldetails.json
def index
@purchases = Purchase.where("quantity - cus1 - cus2 > 0") # Only records with a valid total quantity
end
对于CSV导入,可以在创建Purchase模型对象之前过滤这些值。尝试更改以使用此:
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
if (row["quantity"].to_i - row["cus1"].to_i - row["cus2"].to_i) > 0
Purchase.create! row.to_hash
end
end
end
这将消除CSV导入过程中在应用程序中创建的任何无效(零数量)购买模型对象。这假定标题字段名为“quantity”,“cus1”和“cus2”,可能需要稍微调整以匹配实际的标题字段名称。
答案 1 :(得分:-1)
我遇到了类似的问题,并使用destroy_all
而不是destroy
解决了这个问题。这清除了为我出现的行。如果有帮助,请告诉我。