Rails 5.2 + Trix + ActiveStorage
Rails 5.2 + Trix + ActiveStorage
如何使用配置了 ActiveStorage 的 Rails 5.2 在 Trix 编辑器中上传图像?
我看到一些视频使用其他上传器,但无法将想法适应 ActiveStorage。
其他(也许)解决方案是:将 ActionText 与 Rails 5.2 一起使用。已经可以安全使用了吗?
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}`,
});
}
});
}
});
希望对您有所帮助!
如何使用配置了 ActiveStorage 的 Rails 5.2 在 Trix 编辑器中上传图像?
我看到一些视频使用其他上传器,但无法将想法适应 ActiveStorage。
其他(也许)解决方案是:将 ActionText 与 Rails 5.2 一起使用。已经可以安全使用了吗?
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}`,
});
}
});
}
});
希望对您有所帮助!