Ruby on Rails Thursday, January 24, 2019

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 gem (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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9fa0c674-8830-4f15-8582-def21218fe9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment