我正在关注可排序列表railscast并且一切正常,除非我排序,每次都将相同(原始)顺序传递给update方法,因此它不会更新真正的顺序。
以下是代码:
在视图中:
%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'))
);
排序并显示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
和更新请求:
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)
所以每次我拖放列表,它都会再次发送相同的订单。我错误的是,参数没有通过正确的,更新的订单? 在2757ms完成200 OK(浏览次数:1.0ms | ActiveRecord:6.7ms)
%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;"