在Rails中显示软删除的记录

时间:2019-12-15 17:02:52

标签: ruby-on-rails

我是Ruby on Rails的新秀,目前,我正在开发带有软删除功能的Rails应用程序。正如您在控制器中的索引操作中所看到的那样,作为管理员,我将默认显示不包括软删除记录的记录。

但是我想在页面上打一个复选框或按钮,以在我选中或单击软记录时将其包括在内。而且我很受困于此,如果有人有解决方案,那就太好了。顺便说一句,不要犹豫,为这种情况建议一个更好的方法。任何帮助将不胜感激,我在下面附上了控制器和模型。

控制器

class BookingsController < ApplicationController
  before_action :admin_user, only: %i(edit update)
  before_action :load_tour_detail, only: %i(create reduce_quantity cal_revenue)
  before_action :load_booking, except: %i(index new create index_with_deleted)
  after_action :reduce_quantity, :cal_revenue, only: :create
  after_action :increse_quantity, only: :destroy
  respond_to :html, :json

  def index
    @bookings = if current_user.admin?
                  Booking.includes(:tour_detail).not_deleted
                         .paginate(page: params[:page])
                else
                  Booking.includes(:tour_detail).where(user_id: current_user.id)
                         .paginate(page: params[:page])
                end
  end

模型

class Booking < ApplicationRecord
  belongs_to :tour_detail
  belongs_to :user
  validates :people_number, presence: true
  before_save :cal_price
  enum status: {pending: 1, confirmed: 2, cancelled: 3}
  scope :not_deleted, -> {where("deleted_at IS NULL")}
  scope :deleted, -> {where("deleted_at IS NOT NULL")}

  def soft_delete
    update deleted_at: Time.now
  end

  def recover
    update deleted_at: nil
  end
end

更新: 我找到了解决方案,在索引页面上做了一个按钮,该按钮将参数传递给控制器​​。然后在控制器上检查普拉兰是否有效?并将我的应用重定向到正确的操作,该操作是否包含soft_deleted记录。我将在下面发布解决方案,以防万一我的英文解释太糟糕了。 如果有人对此有更好的解决方案,请提出建议,这对我很有用。谢谢你们!

控制器

class BookingsController < ApplicationController
  before_action :admin_user, only: %i(edit update)
  before_action :load_tour_detail, only: %i(create reduce_quantity cal_revenue)
  before_action :load_booking, except: %i(index new create index_with_deleted)
  after_action :reduce_quantity, :cal_revenue, only: :create
  after_action :increse_quantity, only: :destroy
  respond_to :html, :json

  def index
    @bookings = if current_user.admin?
                  if params.has_key?(:soft_deleted)
                    Booking.includes(:tour_detail).all
                           .paginate(page: params[:page])
                  else
                    Booking.includes(:tour_detail).not_1deleted
                           .paginate(page: params[:page])
                  end
                else
                  Booking.includes(:tour_detail).where(user_id: current_user.id)
                         .paginate(page: params[:page])
                end
  end

查看

<% if params.has_key?(:soft_deleted) %>
   <%= link_to bookings_path, class: "btn btn-sm btn-primary" do %>
     Only existed bookings
   <% end %>
<% else %>
   <%= link_to bookings_path(soft_deleted: true), class: "btn btn-sm btn-warning" do %>
     Included deleted bookings
   <% end %>
<% end %>

1 个答案:

答案 0 :(得分:0)

您可以使用类似以下的内容:

def index
    @bookings = if current_user.admin?
                  Booking.includes(:tour_detail)
                         .deleted(params[:checkbox_input_name]   # ----------> This line here
                         .paginate(page: params[:page])
                else
                  Booking.includes(:tour_detail)
                         .where(user_id: current_user.id)
                         .deleted(params[:checkbox_input_name]   # ----------> This line here

                         .paginate(page: params[:page])
                end
  end

然后在您的model中,添加如下所示的范围:

class Booking < ApplicationRecord
  #   -----------------
  scope :deleted, -> lambda { |deleted_at| where("deleted_at IS NULL") if deleted_at.present? }

 # The above scope will return all objects if deleted_at param is not present. 
  private

  def soft_delete
    update deleted_at: Time.now
  end

  def recover
    update deleted_at: nil
  end
end