我通过 RailsCast 制作了可排序的列 #228 可排序的表列一切都很好。但是当我通过 ajax 排序链接添加销毁记录时,删除后不起作用
application_helper.rb
#application_helper.rb
def sortable(column, title = nil)
title ||= column.titleize
css_class = (column == sort_column) ? "current #{sort_direction}" : nil
direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, page: nil), {:class => css_class, remote: true }
end
contracts_controller.rb
#contracts_controller.rb
def destroy
@contract.destroy
if params[:limit]
@contracts = Contract.includes(:department).order(sort_column + ' ' + sort_direction).page(params[:page]).per(params[:limit])
else
@contracts = Contract.includes(:department).order(sort_column + ' ' + sort_direction).page(params[:page])
end
respond_to do |format|
format.html {}
format.js {flash.now[:danger] = "Success delete"}
end
end
删除链接
<%= link_to 'Delete', contract, remote: true, method: :delete, data: { confirm: 'Are you sure?' } , class: 'btn btn-danger'%>
销毁后进行排序的链接/contracts/1?direction=asc&sort=contractor
,但我需要/contracts?direction=asc&sort=contractor
sortable
帮助程序链接可能必须像这样更改,
link_to title, your_index_path(params.merge(:sort => column, :direction => direction, page: nil)