我正在关注可排序的列表 railscast 并且除了排序时一切正常,每次都将相同的(原始)顺序传递给更新方法,因此它不会更新真实的顺序。
这是代码:
在视图中:
%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
和pieces.js.coffee:
jQuery ->
$('#lineup').sortable(
axis: 'y'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
);
阵容控制器的排序和显示方法:
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
和更新请求:
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)
所以每次我拖放列表时,它都会再次发送相同的订单。参数没有通过正确的更新顺序,我做错了什么?在 2757 毫秒内完成 200 次 OK(查看次数:1.0 毫秒 | ActiveRecord:6.7 毫秒)
%tr{:style => 'height: 60px;'}
%td{:style => 'text-align: center;'}
%h5= index + 1
%td{:style => 'padding-left: 10px;'}
%h5= "#{piece.name} - #{piece.id}"
%td{:style => 'text-align: center;'}
%h5= piece.category
%td{:style => 'padding-left: 10px;'}
%h5= piece.description
%td{:style => 'padding-left: 10px;'}
%h5= @lineup.get_piece_status(piece.id)
%td.pop{:style => 'text-align: center;', :rel => 'popover',:'data-title' => piece.name, :'data-placement' => 'bottom', :'data-content' => image_tag(piece.photo.url(:small))}
= image_tag(piece.photo.url(:tiny))
%td{:style => 'text-align: center;'}
- unless ['Preparing', 'Shipped', 'Arrived'].include?(@lineup.get_piece_status(piece.id))
= form_for(@lineup, :html => {:id => "remove#{piece.id}", :style => "margin-bottom: 0px;"}) do |f|
= f.hidden_field(:piece_id, :value => piece.id)
= link_to 'x', '#', :onclick=>"document.getElementById('remove#{piece.id}').submit(); return false;"