Skip to content

Instantly share code, notes, and snippets.

@dvanderbeek
Last active April 10, 2019 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvanderbeek/15ea1ca570199689b2ba0991e32866e1 to your computer and use it in GitHub Desktop.
Save dvanderbeek/15ea1ca570199689b2ba0991e32866e1 to your computer and use it in GitHub Desktop.
Rails sortable lists with jquery ui
# 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