Rails拖放可分类引导表 - 不保存项目位置

时间:2016-01-29 20:29:28

标签: ruby-on-rails ruby-on-rails-4

考虑在http://benw.me上实施的拖放排序表。表行被移动 - 但是当我重新启动页面时,行的位置不会保留,并恢复为原始行。

错误查询AJAX: screenshot #1

update_row_order error 400

控制器:

def update_row_order
    @task = Task.find(task_params[:task_id])
    @task.row_order_position = task_params[:row_order_position]
    @task.save

    render nothing: true # this is a POST action, updates sent via AJAX, no view rendered
  end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_thing
    @task = Task.find(params[:id])
  end

Tasks.coffee:

jQuery ->
  $('.best_in_place').best_in_place();

jQuery ->
  if $('#sortable').length > 0
    table_width = $('#sortable').width()
    cells = $('.table').find('tr')[0].cells.length
    desired_width = table_width / cells + 'px'
    $('.table td').css('width', desired_width)

    $('#sortable').sortable(
      axis: 'y'
      items: '.item'
      cursor: 'move'

      sort: (e, ui) ->
        ui.item.addClass('active-item-shadow')
      stop: (e, ui) ->
        ui.item.removeClass('active-item-shadow')
        # highlight the row on drop to indicate an update
        ui.item.children('td').effect('highlight', {}, 1000)
      update: (e, ui) ->
        item_id = ui.item.data('item-id')
        console.log(item_id)
        position = ui.item.index() # this will not work with paginated items, as the index is zero on every page
        $.ajax(
          type: 'POST'
          url: '/tasks/update_row_order'
          dataType: 'json'
          data: { thing: {task_id: item_id, row_order_position: position } }
        )
    )

1 个答案:

答案 0 :(得分:2)

遇到同样的问题,请确保在tasks_params中包含:task_id。所以看起来应该是这样的:

def task_params
params.require(:task).permit(:task_id, :other_params, :row_order_position, :other_param)
end