itmammoth / rails_sortable

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

Sort order is not persisted on Heroku (but works on localhost) #23

Closed slead closed 6 years ago

slead commented 6 years ago

Thanks for writing this excellent gem. This is a slightly complicated issue so I don't expect you to debug it for me, but I'm hoping you might have some pointers.

TLDR: do you have any suggestions why the sorting might be persisted on localhost, but not Heroku?

I'm using rails_sortable successfully on a Rails 5.1.5 / Ruby 2.4.0p0 application that's running on my Mac. I have a Posts model with related Photos, to which I added the integer sort column using a Rails migration. When running on localhost I can successfully re-order the photos using drag-and-drop, and the sort order of the Photos is persisted when I save the Post.

The app was already running on Heroku, so I pushed the rails_sortable changes, ran the rake task and database migration, and the app still runs without error.

I can see in the Rails Console that the Photos model has the integer sort column. However, all of the Photos have a null sort value, and although I can drag-and-drop the photos in the view to re-order them, the sort order is not persisted when I save the Post - and after saving the Post, the sort values on the photos are still null.

Do you have any suggestions for how I might debug this? Thanks

Model

class Photo < ApplicationRecord
  has_and_belongs_to_many :posts
  include RailsSortable::Model
  set_sortable :sort
  set_sortable :sort, without_updating_timestamps: true
end

View

%table{:border => 1}
  %tbody.sortable
    -@post.photos.order(:sort).each_with_sortable_id do |photo, sortable_id|
      %tr{:id => sortable_id}
        %td
          %img{:src => photo.small, :width => "100"}/
        %td=photo.title

application.js

function pageLoad() {
    $('.sortable').railsSortable();
}

$(document).on('turbolinks:load', pageLoad);
slead commented 6 years ago

I discovered that the problem only applied to existing photos, where the sort value was null. If I created a new photo, and/or manually calculated the existing photos' sort values to 0, the plugin worked as expected.

Thanks again for writing this excellent tool.