itmammoth / rails_sortable

Easy drag & drop sorting with persisting the arranged order for rails
MIT License
143 stars 37 forks source link

incorrect JSON in SortableController#reorder #57

Closed birthdaycorp closed 3 years ago

birthdaycorp commented 3 years ago

Edit: problem fixed. sorry for using this as a StackOverflow. Problem was I had <% sortable_id %> and not <%= sortable_id %> in the view.

My setup has a bunch of MediaFiles that belong to a polymorphic association called fileable:

class MediaFile < ApplicationRecord
  include MediaFileUploader::Attachment(:file)
  include RailsSortable::Model
  set_sortable :sort
  belongs_to :fileable, polymorphic: true

  validates_presence_of :file
end

And my view follows the docs:

<%= form_for @item, html: { enctype: "multipart/form-data" } do |f| %>
  <div class="ui two column grid">
    <div class="ten wide column">
      <ul class="sortable">
        <% @media_files.each_with_sortable_id do |media_file, sortable_id| %>
          <li id="<% sortable_id %>"><%= image_tag media_file.file_url(:small) %></li>
        <% end %>
      </ul>
    </div>
  </div>
<% end %>

However, dragging and dropping the elements produces this output:

Started POST "/sortable/reorder" for 2600:1700:ba00:3970:fdcc:77d6:4747:4d44 at 2021-07-29 18:23:33 -0700
Processing by SortableController#reorder as JSON
  Parameters: {"rails_sortable"=>["", "", ""], "sortable"=>{"rails_sortable"=>["", "", ""]}}

The sort and updated_at attributes are not being updated the way they should. Has anyone else had this problem, and what did you do?