Last active
April 10, 2019 15:17
-
-
Save dvanderbeek/15ea1ca570199689b2ba0991e32866e1 to your computer and use it in GitHub Desktop.
Rails sortable lists with jquery ui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make sure to add :position to permitted params in questions controller | |
# Sortable items need data-update-url and data-param-key attributes | |
# coffeescript | |
$(document).on 'turbolinks:load', -> | |
$('.sortable').sortable | |
axis: 'y' | |
handle: '.handle' | |
update: (e, ui) -> | |
data = new FormData | |
data.append "#{ui.item.data('param-key')}[position]", ui.item.index() + 1 | |
Rails.ajax | |
url: ui.item.data('update-url') | |
type: "PATCH" | |
data: data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment