sortable list using jquery ui not passing updated list order

Posted by Matthew Berman on Stack Overflow See other posts from Stack Overflow or by Matthew Berman
Published on 2012-06-25T03:12:51Z Indexed on 2012/06/25 3:15 UTC
Read the original article Hit count: 156

I am following the sortable lists railscast and got everything working except when I sort, the same (original) order gets passed to the update method each time, so it doesn't update the true order.

Here's the code:

In the view:

%tbody#lineup{"data-update-url" => sort_lineups_url}
      - @lineup.pieces.each_with_index do |piece, index|
        = content_tag_for :tr, piece do
          = render 'piece', :piece => piece, :index => index

and pieces.js.coffee:

jQuery ->
  $('#lineup').sortable(
    axis: 'y'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
  );

sort and show methods of lineupscontroller:

  def show
    @lineup = Lineup.find_by_user_id(current_user.id)
    @pieces = @lineup.pieces.order("position")
  end

  def sort
    params[:piece].each_with_index do |id, index|
      current_user.lineup.pieces.update_all({position: index+1}, {id: id})
    end
    render nothing: true
  end

and the update request:

Started POST "/lineups/sort" for 127.0.0.1 at 2012-06-24 20:06:14 -0700
Processing by LineupsController#sort as */*
  Parameters: {"piece"=>["8", "2", "1", "4", "3", "7"]}
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  Lineup Load (0.6ms)  SELECT `lineups`.* FROM `lineups` WHERE `lineups`.`user_id` = 2 LIMIT 1
  SQL (2.9ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 1 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 8
  SQL (0.6ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 2 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 2
  SQL (0.6ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 3 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 1
  SQL (0.5ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 4 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 4
  SQL (0.6ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 5 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 3
  SQL (0.5ms)  UPDATE `pieces` INNER JOIN `piece_lineups` ON `pieces`.`id` = `piece_lineups`.`piece_id` SET `position` = 6 WHERE `piece_lineups`.`lineup_id` = 3 AND `pieces`.`id` = 7
  Rendered text template (0.0ms)

so each time i drag+drop the list, it sends the same order again. what am I doing wrong that the params isn't passing the right, updated order? Completed 200 OK in 2757ms (Views: 1.0ms | ActiveRecord: 6.7ms)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about jquery-ui