【问题标题】:Rails. SmartListing gem doesn't refresh table after destroying item导轨。 SmartListing gem 在销毁项目后不刷新表
【发布时间】:2017-02-23 16:15:15
【问题描述】:

我有一个包含类别表的页面。我正在使用 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 %>

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2013-10-24
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多