【发布时间】:2018-12-03 02:14:33
【问题描述】:
我正在尝试使用 Active Storage 来管理图像。我让多个上传工作正常,但我想使用acts_as_list 位置和jQuery 可排序来使用ajax 管理他们的订单,比如the video on this site。在普通模型中,我会运行迁移以向模型添加位置。但看起来我无法访问 Blob 或 Attachement 模型。我将如何解决这个问题?还是我必须重新使用 Carrierwave?任何帮助将不胜感激。
为了澄清,让我添加代码。
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Content:</strong>
<%= @post.content %>
</p>
<% if @post.images.attached? %>
<p>
<strong>Images:</strong>
<br>
<div id="images" class="list-group" data-url="<%= sort_posts_path %>">
<% @post.images.order(:position).each do |image| %>
<%= link_to image_tag image.variant(resize: "200x200"), class: "list-group-item", id: dom_id(image) %>
<% end %>
</div>
</p>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
posts.js
document.addEventListener("turbolinks:load", function() {
$("#images").sortable({
update: function(e, ui) {
/* Update active storage database entry for each POST image. */
/* This is the RAILS AJAX from the tutorial */
Rails.ajax({
url: $(this).data("url"),
type: "PATCH",
data: $(this).sortable('serialize'),
}
});
});
routes.rb
Rails.application.routes.draw do
resources :posts do
collection do
patch :sort
end
end
end
posts_controller.rb
def sort
params[:image].each_with_index do |id, index|
Post.image.where(id: id).update_all(position: index + 1)
end
head :ok
end
【问题讨论】:
标签: ruby-on-rails rails-activestorage