Mongoid不会更新位置字段

问题描述 投票:0回答:1

我希望能够更新Attachinary :: File位置字段

我使用ruby 2.5.0,rails 5.2.2,mongoid,'jquery-ui-rails'和自定义Attachinary(https://github.com/ipatovanton/attachinary/tree/position)进行图片上传。

的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

当我尝试拖动图像时,我收到下一条消息。但该职位没有得到更新:

启动PATCH“/ projects / sort”为127.0.0.1在2019-01-23 18:19:46 +0300由ProjectsController处理#s排成/参数:{“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 |开始了| {“find”=>“users”,“filter”=> {“_ id”=> BSON :: ObjectId('5c472c2e1996da1d037f57fb')},“sort”=> {“_ id”=> 1},“limit”= > 1,“singleBatch”=> true,“lsid”=> {“id”=>}} MONGODB | localhost:27017 | squarely_development.find |成功| 0.001s在6ms内完成200 OK

如果我使用ActiveRecord和gem'pg'一切正常

但是我需要这个解决方案来使用Mongodb

对此有任何想法或想法的人?

谢谢

ruby-on-rails ruby mongodb mongoid
1个回答
0
投票

首先,您似乎正在尝试对项目的图像进行排序。

如果这是真的,那么我建议将此路线移至成员而不是集合。

这意味着你将不得不添加sort_project_path(@project)。如果您以与访问图像相同的方式检索图像,这可以使我们的生活更轻松。

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

我希望这对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.