Rails应用中CoffeeScript拖拽功能数组声明错误求助
Hey there! Let's work through that CoffeeScript bug in your Rails drag-and-drop setup. I can spot a few syntax and structure issues in your code snippet—let's break them down and fix things step by step.
First, let's identify the key problems:
- Truncated array push syntax: The line
updated_order.push id: $(this)...is incomplete. CoffeeScript requires explicit, properly formatted object literals when pushing to arrays, and the ellipsis here is causing a syntax error. - Redundant
sortable()calls: You're initializingsortable()twice on the same selector—this is unnecessary and can cause unexpected behavior. - Unnecessary variable initialization: The line
ready = undefined set_positions = undefinedis redundant in CoffeeScript; variables are automatically hoisted, so you can jump straight to assigning your functions.
Here's the fixed, clean version of your CoffeeScript:
set_positions = -> $('.position').each (i) -> $(this).attr 'data-pos', i + 1 ready = -> set_positions() # Initialize sortable once, then bind the update event $('.sortable').sortable().on 'sortupdate', (e, ui) -> updated_order = [] # Refresh positions and build the updated order array set_positions() $('.position').each (i) -> # Replace `data('id')` with the correct attribute for your model IDs updated_order.push id: $(this).data('id') position: i + 1 # Optional: Send the updated order to your Rails backend via AJAX $.ajax url: '/your/update/path' # Update this to your actual endpoint type: 'PUT' data: { order: updated_order } $(document).ready ready $(document).on 'turbolinks:load', ready # Add this if you're using Turbolinks
Let's walk through the fixes:
- Fixed array push: I completed the object literal in
updated_order.pushto include both theid(pulled from the element'sdata-idattribute—adjust this to match how your elements store model IDs) and the newposition. - Simplified sortable setup: We initialize
sortable()once, then chain theon('sortupdate')event binding instead of callingsortable()twice. - Cleaned up variable declarations: Removed the redundant
undefinedinitializations—CoffeeScript handles variable scope automatically, so we can define our functions directly. - Added Turbolinks support: If your Rails app uses Turbolinks, adding the
turbolinks:loadevent ensures your code runs correctly when navigating between pages.
Quick checks to verify:
- Make sure each
.positionelement has adata-idattribute (or adjust theid: $(this).data('id')line to match how you're storing record IDs on the element, e.g.,$(this).attr('id').split('-').pop()if using Rails' default ID format likeitem-123). - Check your browser's developer console for any remaining syntax errors—CoffeeScript compiles to JavaScript, so console errors will point you to any remaining issues.
- Test the drag-and-drop behavior to confirm the
updated_orderarray is being built correctly (you can add aconsole.log(updated_order)inside thesortupdatehandler to inspect it).
内容的提问来源于stack exchange,提问作者script
相关产品推荐
相关产品推荐

