ruby-on-rails - Mongoid 不更新位置字段
问题描述
我希望能够更新 Attachinary::File 位置字段
我使用 ruby 2.5.0、rails 5.2.2、mongoid、'jquery-ui-rails' 和 自定义附件( https://github.com/ipatovanton/attachinary/tree/position ) 来上传图片。
应用程序.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'),
});
}
});
});
})
路线.rb
resources :projects do
collection do
patch :sort
end
end
项目.rb
class Project
include Mongoid::Document
has_attachments :images
end
显示.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>
项目控制器.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
当我尝试拖动图像时,我会收到下一条消息。但是职位没有更新:
2019-01-23 18:19:46 +0300 为 127.0.0.1 启动 PATCH "/projects/sort" 由 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 | 本地主机:27017 | squarely_development.find | 开始 | {"
如果我使用 ActiveRecord 和 gem 'pg' 一切正常
但我需要这个解决方案来使用 Mongodb
有人对此有任何想法或想法吗?
谢谢
解决方案
首先,您似乎正在尝试对项目的图像进行排序。
如果这是真的,那么我建议将这条路线移动到成员而不是集合上。
这意味着您必须添加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
我希望这对你有用。
推荐阅读
- python - 为什么我不能导入 load_dotenv?
- symfony - 给定对象不是该类的实例,当尝试在 PHPUnit 测试中设置成员变量时,此方法被错误声明
- python - 如何在 colab 中加载 matlab 文件?
- python - 如何在 Python 中迭代类对象并更改属性值
- c# - 在winforms中将数据加载到实体框架dll中的问题
- python - 单击时鼠标的坐标 - Python
- java - 插入文档时如何调用 MongoRepository (Spring Data) 中的存储函数?
- java - 禁用或避免假 gps
- c++ - 如何创建始终在线?
- mysql - 如何选择 3 个表格并显示 orderby date