0
votes

I want to be able update Attachinary::File position field

I use ruby 2.5.0, rails 5.2.2, mongoid, 'jquery-ui-rails' and custom Attachinary (https://github.com/ipatovanton/attachinary/tree/position) for images upload.

application.js

jQuery(function() {
  $(document).on('turbolinks:load', function(){
    $('.attachinary-input').attachinary()

    $("#images").sortable({
      update: function(e, ui) {
        Rails.ajax({
          url: $(this).data("url"),
          type: "PATCH",
          data: $(this).sortable('serialize'),
        });
      }
    });
  });
})

routes.rb

resources :projects do
    collection do
      patch :sort
    end
end

project.rb

class Project
  include Mongoid::Document

  has_attachments :images
end

show.html.erb

<div id="images" class="grid" data-url="<%= sort_projects_path %>">
  <% @project.images.order(position: :desc).each do |image| %>
    <div id="image_<%= image.id %>" class="box">
      <div class="box-image">
        <%= cl_image_tag(image.path, width: '250', height: '250', crop: 'thumb') %>
      </div>
    </div>
  <% end %>
</div>

projects_controller.rb

class ProjectsController < ApplicationController
  def sort
    params[:image].each_with_index do |id, index|
      Attachinary::File.where(id: id).update_all(position: index + 1)
    end
    head :ok
  end
end

When I try to drag an image, I receive the next message. But the position is not getting updated :

Started PATCH "/projects/sort" for 127.0.0.1 at 2019-01-23 18:19:46 +0300 Processing by ProjectsController#sort as / Parameters: {"image"=>["5c4827691996da1fef832f5d", "5c4827691996da1fef832f6e", "5c4827691996da1fef832f5e", "5c4827691996da1fef832f5f", "5c4827691996da1fef832f60", "5c4827691996da1fef832f61", "5c4827691996da1fef832f62", "5c4827691996da1fef832f63", "5c4827691996da1fef832f64", "5c4827691996da1fef832f65", "5c4827691996da1fef832f66", "5c4827691996da1fef832f67", "5c4827691996da1fef832f68", "5c4827691996da1fef832f69", "5c4827691996da1fef832f6a", "5c4827691996da1fef832f6b", "5c4827691996da1fef832f6c", "5c4827691996da1fef832f6d", "5c4827691996da1fef832f5c"]} MONGODB | localhost:27017 | squarely_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('5c472c2e1996da1d037f57fb')}, "sort"=>{"_id"=>1}, "limit"=>1, "singleBatch"=>true, "lsid"=>{"id"=>}} MONGODB | localhost:27017 | squarely_development.find | SUCCEEDED | 0.001s Completed 200 OK in 6ms

If I use ActiveRecord and gem 'pg' everything works

But I need this solution to work with Mongodb

Anyone with any thoughts or ideas on this?

Thanks

1
what is your mongoid version?Oshan Wisumperuma
before rendering show page, try project.reload_relationsOshan Wisumperuma
@Oshanz mongoid 7.0.2Anton Ipatov
@Oshanz reload_relations no effectAnton Ipatov
file = Attachinary::File.find(id); file.position = index + 1; file.save! will this work for youOshan Wisumperuma

1 Answers

1
votes

First of all, it seems that you are trying to sort images of a project.

If that is true, then I suggest moving this route to be on member rather than the collection.

That means you will have to add sort_project_path(@project). That can make our life easier given you retrieve the images in the same way you access them.

class ProjectsController < ApplicationController
  before_action :set_project # You may already have that

  def sort
    params[:image].each_with_index do |id, index|
      @project.images.where(id: id).update_all(position: index + 1)
    end
    head :ok
  end

  def set_project
    @project = Project.find(params[:id])
  end
end

I hope that works for you.