Ruby on rails Mongoid不';t更新位置字段
我希望能够更新附件::文件位置字段 我使用ruby 2.5.0、rails 5.2.2、mongoid、“jQueryUIRails”和自定义Attachinary()上传图像 application.jsRuby on rails Mongoid不';t更新位置字段,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,我希望能够更新附件::文件位置字段 我使用ruby 2.5.0、rails 5.2.2、mongoid、“jQueryUIRails”和自定义Attachinary()上传图像 application.js jQuery(function() { $(document).on('turbolinks:load', function(){ $('.attachinary-input').attachinary() $("#images").sortable({ u
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\u 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
class ProjectsController
当我尝试拖动图像时,我会收到下一条消息。但这一立场并未得到更新:
2019-01-23 18:19:46为127.0.0.1启动补丁“/projects/sort”
+0300项目控制器的处理#排序为/参数:{“image”=>[“5C482761996DA1FE832F5D”,“5C482761996DA1FE832F6E”,
“5C4827691996DA1EF832F5E”、“5C4827691996DA1EF832F5F”,
“5C4827691996DA1EF832F60”、“5C4827691996DA1EF832F61”,
“5C4827691996DA1EF832F62”、“5C4827691996DA1EF832F63”,
“5C4827691996DA1EF832F64”、“5C4827691996DA1EF832F65”,
“5C4827691996DA1EF832F66”、“5C4827691996DA1EF832F67”,
“5C4827691996DA1EF832F68”、“5C4827691996DA1EF832F69”,
“5C4827691996DA1EF832F6A”、“5C4827691996DA1EF832F6B”,
“5C4827691996DA1EF832F6C”、“5C4827691996DA1EF832F6D”,
“5c4827691996da1fef832f5c”]}MONGODB |本地主机:27017|
squarly_development.find |启动|{“find”=>“用户”,
“过滤器”=>{“\u id”=>BSON::ObjectId('5c472c2e1996da1d037f57fb'),
“sort”=>{u id”=>1},“limit”=>1,“singleBatch”=>true,
“lsid”=>{“id”=>}}MONGODB|本地主机:27017|
squarely_development.find |成功| 0.001s在6ms内完成200 OK
如果我使用ActiveRecord和gem'pg',一切正常
但是我需要这个解决方案来与Mongodb合作
有人对此有什么想法或想法吗
谢谢首先,您似乎正在尝试对项目的图像进行排序 如果这是真的,那么我建议将此路线移动到会员而不是集合上 这意味着您必须添加
排序项目路径(@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
class ProjectsController
我希望这对您有用。您的mongoid版本是什么?在呈现show页面之前,请尝试
project.reload\u relations
@Oshanz mongoid 7.0。2@Oshanzreload_关系
no-effectfile=Attachinary::File.find(id);file.position=index+1;文件。保存!这对你有用吗