Skip to content

Instantly share code, notes, and snippets.

@stympy
Created October 31, 2008 18:24
Show Gist options
  • Save stympy/21378 to your computer and use it in GitHub Desktop.
Save stympy/21378 to your computer and use it in GitHub Desktop.
<table>
<thead>
<tr><th>Name</th></tr>
</thead>
<tbody id="specifications">
<% @specifications.each do |spec| %>
<tr id="spec-<%= spec.id %>"><td><%= spec.name %></td></tr>
<% end %>
</tbody>
</table>
<% content_for :head do -%>
<%= javascript_include_tag 'jquery.js', 'jquery.tablednd_0_5.js' %>
<script type="text/javascript" charset="utf-8">
$().ready(function() {
$('#specifications').tableDnD({
onDrop: function(table, row) {
$.ajax({
type: "POST",
url: "<%= url_for(:action => 'sort') %>",
processData: false,
data: $.tableDnD.serialize() + '&authenticity_token=' + encodeURIComponent('<%= form_authenticity_token if protect_against_forgery? %>'),
success: function(msg) {
alert("The specifications have been updated")
}
});
}
})
})
</script>
<% end -%>
SpecificationsController < ActionController::Base
def sort
Specification.all.each do |spec|
if position = params[:specifications].index(spec.id.to_s)
spec.update_attribute(:position, position + 1) unless spec.position == position + 1
end
end
render :nothing => true, :status => 200
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment