Mongoid 不更新位置字段
Mongoid doesn't update position field
我希望能够更新 Attachinary::File 位置字段
我使用 ruby 2.5.0、rails 5.2.2、mongoid、'jquery-ui-rails' 和 自定义附件(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
当我尝试拖动图像时,我收到了下一条消息。但是位置没有得到更新:
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
如果我使用 ActiveRecord 并且 gem 'pg' 一切正常
但我需要这个解决方案才能与 Mongodb
一起使用
有人对此有任何想法或想法吗?
谢谢
首先,您似乎正在尝试对项目的图像进行排序。
如果那是真的,那么我建议将这条路线移动到会员而不是 collection。
这意味着您必须添加 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
希望对你有用。
我希望能够更新 Attachinary::File 位置字段
我使用 ruby 2.5.0、rails 5.2.2、mongoid、'jquery-ui-rails' 和 自定义附件(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
当我尝试拖动图像时,我收到了下一条消息。但是位置没有得到更新:
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
如果我使用 ActiveRecord 并且 gem 'pg' 一切正常
但我需要这个解决方案才能与 Mongodb
一起使用有人对此有任何想法或想法吗?
谢谢
首先,您似乎正在尝试对项目的图像进行排序。
如果那是真的,那么我建议将这条路线移动到会员而不是 collection。
这意味着您必须添加 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
希望对你有用。