I have page with table of categories. I'm using SmartListing GEM for sorting data and also I added some buttons for each item in table, including "delete" button.
Problem: after element in the table was deleted, table blinks but doesn't refresh data, so I need to reload hole page to get updated table. (By the way, element removed from the database without problems)
My files look like the files in developer example.
My files:
index and destroy actions in 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 %>
Problem: after element in the table was deleted, table blinks but doesn't refresh data, so I need to reload hole page to get updated table. (By the way, element removed from the database without problems)
I found a solution to the problem. It was resolved simply - just had to add to the destroy action line redirect_to action: 'index', status: 303
, so this action should look like this:
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