【发布时间】:2021-04-23 23:58:41
【问题描述】:
为活动存储附件实现拖放。一些代码来自 goRails,一些来自我的大脑/网络。
因为我们要订购附件,所以我继续创建了一个模型和控制器来表示它们,因为安装 activestorage 没有。
我正在使用“acts_as_list”,因此需要一个模型来添加acts_as_list 钩子。
所以我有:
models/active_storage_attachment.rb
class ActiveStorageAttachment < ApplicationRecord
acts_as_list
end
控制器/active_storage_attachments_controller.rb
class ActiveStorageAttachmentsController < ApplicationController
before_action :set_active_storage_attachment, only: [:move]
def move
@active_storage_attachment.insert_at(params[:position].to_i)
head :ok
end
private
def set_active_storage_attachment
@active_storage_attachment = ActiveStorageAttachment.find(params[:id])
end
end
我还添加了一条路线:
resources :active_storage_atachments do
member do
patch :move
end
end
sortablejs通过js调用ajax请求的路由很简单。
电话最终是这样的:
https://myamazonurl.com/active_storage_attachments/96/move
Rails 告诉我不存在路线,但如果我运行 rails 路线,我会看到该死的路线。
move_active_storage_attachment_path PATCH /active_storage_attachments/:id/move(.:format)
active_storage_attachments#move
我认为这只是我麻烦的开始,但我无法弄清楚为什么最简单的部分,即路线,不起作用。
【问题讨论】:
标签: ruby-on-rails rails-activestorage sortablejs