我通过RailsCast#228可排序表列制作了可排序的列 一切顺利。 但是当我通过ajax添加销毁记录时排序链接在删除后不起作用
#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
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
答案 0 :(得分:1)
sortable
帮助链接可能必须像这样改变,
link_to title, your_index_path(params.merge(:sort => column, :direction => direction, page: nil)