jQuery を使用してテーブルをソート可能 (ドラッグ アンド ドロップ) にしようとしています。問題は、ソートされた新しいテーブルを保存してから、他のユーザーに公開するか、ブラウザーを更新したときです。
私は使用しています:
宝石 'acts_as_list' Rails 3.2.17 ルビー 1.9.3p484
これを行うより良い方法はありますか?
コントローラーのソートおよび表示メソッド
def sort @episodes = current_user.episodes_for(@show).each do |episode| episode.position = params['episode'].index(episode.id.to_s) + 1 episode.save end render :nothing =>true end
def show
@episodes = current_user.episodes_for(@show)
@episodes.order('episode.position ASC')
@episode = @episodes.where(id: params[:id]).first
end
私のJavascript
<script>
$(window).load(function() {
$("#sortthis").sortable({
axis: 'y',
placeholder: 'ui-state-highlight',
cursor: 'move',
dropOnempty: false,
scroll: true,
opacity: 0.4,
update: function(event, ui){
var itm_arr = $("#sortthis").sortable('toArray');
var pobj = {episodes: itm_arr};
$.post("/episodes/sort", pobj);
}
});
});
</script>
ビューで並べ替えたいテーブル:
<tbody id='eps'>
<tr>
<th>Title</th>
<th>#</th>
<th>Season</th>
<th>Download?</th>
<th>Shared?</th>
<th>Length</th>
<th>Status</th>
<th></th>
</tr>
<% for episode in @episodes %>
<tr>
<td id="eps_<%= episode.id %>", width="20%"><%=h episode.title %></td>
<td id="eps_<%= episode.id %>"><%=h episode.number %></td>
<td id="eps_<%= episode.id %>"><%=h episode.season %></td>
<td id="eps_<%= episode.id %>"><%=h episode.is_downloadable %></td>
<td id="eps_<%= episode.id %>"><%=h episode.shared_with_dev %></td>
<td id="eps_<%= episode.id %>"><%=h episode.length %></td>
<td>
<% if episode.video %>
<%= link_to 'Replace Video', new_show_episode_video_path(episode.show, episode) %>
<% else %>
<%= link_to 'Upload Video', new_show_episode_video_path(episode.show, episode) %>
<% end %>
</td>
<td>
<%= link_to "View", [episode.show, episode] %>
<%= link_to "Edit", edit_show_episode_path(episode.show, episode) if permitted_to? :update, episode %>
<%= link_to "Delete", show_episode_path(episode.show, episode), :confirm => 'Are you sure?', :method => :delete if permitted_to? :delete, episode %>
</td>
</tr>
<% end %>
どんな助けでも大歓迎です!