轨。销毁项

时间:2017-02-23 16:15:15

标签: ruby-on-rails ruby smart-listing

我有包含类别表的页面。我正在使用SmartListing GEM对数据进行排序,还为表中的每个项目添加了一些按钮,包括“删除”按钮。

问题: 删除表中的元素后,表闪烁但不刷新数据,所以我需要重新加载漏洞页面以获取更新的表。 (顺便说一句,元素从数据库中删除没有问题)

我的文件看起来像developer example中的文件。

我的档案:

CategoriesController

中的索引和销毁操作
def index
  @categories = Category.all
  smart_listing_create :categories, @categories,
                       sort_attributes: [
                         [:created_at, 'categories.created_at'],
                         [:updated_at, 'categories.updated_at'],
                         [:name, 'categories.name'],
                         [:calculate, 'categories.issues_count']
                       ],
                       default_sort: { name: 'asc' }, partial: 'category'
end

def destroy
  @category = Category.find(params[:id])
  if @category.issues.empty?
    @category.destroy
    flash[:success] = 'Deleted'
  else
    flash[:alert] = 'Category isn\'t empty!'
  end
end

index.html.slim

#heading-breadcrumbs
  .container
    .row
      .col-md-7
        h1=title 'Categories'
      .col-md-5
        ul.breadcrumb
          li= link_to 'Main', root_path
          li Categories
#content
  .container
    .row
      #categories-moderation.col-md-12.col-sm-12.col-xs-12
        = link_to 'Create new', new_account_admin_category_path, class: "newcategory btn btn-lg btn-info"
        = smart_listing_render(:categories)

_category.html.slim

- unless smart_listing.empty?
  .table-responsive
    table.table.table-hover.flex-table
      thead
        tr.centered
          th.col-md-2.col-sm-3
            = smart_listing.sortable 'Created', :created_at
          th.col-md-2.col-sm-3
            = smart_listing.sortable 'Updated', :updated_at
          th.col-md-4.col-sm-3
            = smart_listing.sortable 'Title', :name
          th.col-md-2.col-sm-2.hidden-title
            = smart_listing.sortable 'Issues', :calculate
          th.col-md-2.col-sm-2.hidden-title
            | Actions
      tbody
        - smart_listing.collection.each do |category|
          tr.editable[data-id=category.id]
            = smart_listing.render object: category, partial: 'category_item', locals: {object: category}
  = smart_listing.paginate
- else
  p Empty

_category_item.html.slim

tr.centered
  td.col-md-2.col-sm-3.col-xs-12.flex-order-0
    span.user-label Created:
    = object.created_at.strftime("%m.%d.%Y, %T")
  td.td.col-md-2.col-sm-3.col-xs-12.flex-order-1
    span.user-label Updated:
    = object.updated_at.strftime("%m.%d.%Y, %T")
  td.lefted.col-md-4.col-sm-2.col-xs-12.flex-order-2
    = link_to object.name, object
  td.issues_count.col-md-2.col-sm-5.col-xs-12.flex-order-3
    span.user-label Issues:
    = render partial: 'shared/number_of_issues', locals: { id: object.id }
  td.actions.col-md-4.col-sm-5.col-xs-12.flex-order-4
    = smart_listing_item_actions [{name: :edit,
                                   url: edit_account_admin_category_path(object),
                                   icon: 'glyphicon glyphicon-pencil',
                                   title: 'Edit'},
                                  {name: :destroy,
                                   url: account_admin_category_path(object),
                                   confirmation: 'Sure?',
                                   icon: 'glyphicon glyphicon-trash',
                                   remote: true, if: object.issue_ids.empty?,
                                   title: 'Delete'}]

index.js.erb的

<%= smart_listing_update(:categories) %>

update.js.erb

<%= smart_listing_item :categories, :update, @category, 'category_item' %>

destroy.js.erb

<%= smart_listing_item :categories, :destroy, @category %>

问题: 删除表中的元素后,表闪烁但不刷新数据,所以我需要重新加载漏洞页面以获取更新的表。 (顺便说一句,元素从数据库中删除没有问题)

1 个答案:

答案 0 :(得分:1)

我找到了问题的解决方案。它只是简单地解决了 - 只需添加到 destroy 操作行redirect_to action: 'index', status: 303,所以此操作应如下所示:

def destroy
  @category = Category.find(params[:id])
  if @category.issues.empty?
    @category.destroy
    flash[:success] = 'Deleted'
    redirect_to action: 'index', status: 303
  else
    flash[:alert] = 'Category isn\'t empty!'
  end
end