简体   繁体   English

隐藏记录而不是在rails 4.2.0上的ruby中删除它们

[英]Hide records rather than delete them in ruby on rails 4.2.0

I would like to hide some of the records of the statement rather than deleting them completely in order to retrieve the respective balance amount after the temporary hide. 我想隐藏声明的一些记录,而不是完全删除它们,以便在临时隐藏后检索相应的余额。

Please refer the screenshot for better understanding as shown below; 请参阅屏幕截图以获得更好的理解,如下所示;

screenshot.png

How would I proceed? 我该怎么办?

index.html.erb 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 _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 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 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 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

Any suggestions are most welcome. 任何建议都是最受欢迎的。

Thank you in advance. 先感谢您。

EDIT: This answer is invalid as it assumes that the OP wants to permanently archive records. 编辑:此答案无效,因为它假定OP想要永久存档记录。

Use scopes 使用范围

  1. Add a new field archived to your table in the usual way. 以通常的方式添加archived到您的表的新字段。 I assume you know how to do that. 我猜你知道怎么做。
  2. Instead of deleting rows, set some_xvaziri.archived=true to flag the record(s) you do not want to show anymore. 不要删除行,而是设置some_xvaziri.archived=true来标记您不想再显示的记录。
  3. In your model, add a "default scope" (see http://apidock.com/rails/ActiveRecord/Base/default_scope/class ) 在您的模型中,添加“默认范围”(请参阅http://apidock.com/rails/ActiveRecord/Base/default_scope/class

     class Xvaziri < ActiveRecord::Base default_scope { where(:archived => false) } end 

    This will remove those records from all rails queries (unless you explicitely tell rails to ignore the default scope for a query). 这将从所有rails查询中删除这些记录(除非您明确告诉rails忽略查询的默认范围)。

Here's a practical help for adding the hidden field: 这是添加隐藏字段的实用帮助:

Add a migration with typing this in your console: 在控制台中输入以下内容添加迁移:

rails g migration AddHiddenToXvaziris hidden:boolean

Open the generated migration file and change the following line: 打开生成的迁移文件并更改以下行:

add_column :xvaziris, :hidden, :boolean, :default => false

Save the file und run the migration: 保存文件并运行迁移:

rake db:migrate

Then you have to check whether the hidden-attribute is false and add a row to @xvaziris only if hidden is false. 然后你必须检查hidden-attribute是否为false,并且只有在hidden为false时才向@xvaziris添加一行。 In your xvaziris_controller.rb simply change the index method like this: 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

You also have to set the hidden attribute to true when the destroy action is called. 调用destroy操作时,还必须将hidden属性设置为true。 in your xvaziris_controller.rb change the destroy method: 在你的xvaziris_controller.rb中更改destroy方法:

def destroy
    @xvaziri = Xvaziri.find(params[:id])
    @xvaziri.hidden = true
    flash[:notice] = 'Xvaziri was successfully hidden.'
    redirect_to xvaziris_url    
end

Note, that elements aren't destroyed now, but are set to hidden == true. 请注意,元素现在不会被销毁,但设置为hidden == true。 In case you want elements to be destroyed elsewhere, I recommend to leave the destroy method as it is and add a new method in your xvaziris_controller.rb: 如果你想在其他地方销毁元素,我建议保留destroy方法,并在你的 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

When you do so you also have to add the following line in your routes: 执行此操作时,您还必须在路线中添加以下行:

get '/xvaziri/:id/hide', to: 'xvaziris#hide_xvaziri'

You can customize the route, if you want, to fit your needs. 您可以根据需要自定义路线以满足您的需求。


To show the hidden elements up again I recommend to do the following: Add a "Show all"-Button to your index.html.erb : 要再次显示隐藏的元素,我建议您执行以下操作:在index.html.erb中添加“全部显示”-Button:

<%= link_to "Show all", resetfilter_xvaziri_path %>

Then you must add to you routes.rb this line: 然后你必须添加routes.rb这一行:

get '/xvaziri/resetfilter', to: 'xvaziris#reset_filter'

In your xvaziris_controller.rb at this method: 在此方法的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

You can introduce new field hidden that is by default false , so all of your existing data will be shown. 您可以引入hidden新字段,默认为false ,因此将显示所有现有数据。 Then for every record you want to hide set this field(flag) to true . 然后,对于要隐藏的每个记录,将此字段(标志)设置为true

Implement your methods for balance to include or exclude hidden records. 实施平衡方法以包含或排除隐藏记录。

Go through this link 浏览此链接

ALso there is this gem act-as-paranoid which does the same thing 此外,还有这种宝石作为偏执狂做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM