並べ替え可能なリストの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で200OKを完了(ビュー: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;"