【发布时间】:2019-05-19 01:02:28
【问题描述】:
如何在配置了 ActiveStorage 的 Rails 5.2 的 Trix 编辑器中上传图片?
我看到一些使用其他上传器的视频,但无法将这个想法适应 ActiveStorage。
其他(可能)解决方案是:将 ActionText 与 Rails 5.2 一起使用。已经可以安全使用了吗?
【问题讨论】:
标签: ruby-on-rails rails-activestorage
如何在配置了 ActiveStorage 的 Rails 5.2 的 Trix 编辑器中上传图片?
我看到一些使用其他上传器的视频,但无法将这个想法适应 ActiveStorage。
其他(可能)解决方案是:将 ActionText 与 Rails 5.2 一起使用。已经可以安全使用了吗?
【问题讨论】:
标签: ruby-on-rails rails-activestorage
Active Storage 有直接上传js,只需添加:
//= require activestorage
到你的 application.js,然后创建 trix-attachment-add 事件监听器:
document.addEventListener('trix-attachment-add', function (event) {
var file = event.attachment.file;
if (file) {
var upload = new window.ActiveStorage.DirectUpload(file,'/rails/active_storage/direct_uploads', window);
upload.create((error, attributes) => {
if (error) {
return false;
} else {
return event.attachment.setAttributes({
url: `/rails/active_storage/blobs/${attributes.signed_id}/${attributes.filename}`,
href: `/rails/active_storage/blobs/${attributes.signed_id}/${attributes.filename}`,
});
}
});
}
});
希望对您有所帮助!
【讨论】: