如何解决如何通过Trix将上传的对象命名为Amazon S3?
因此,我在Rails应用程序上设置了活动存储和操作文本。我在我的WYSISYG编辑器中使用了trix,仅将图像放入编辑器中,它就被升格为S3。 这是我的设置:
class Story < ApplicationRecord
has_rich_text :content
belongs_to :industry
...ommited for brevity
在我看来(我正在使用ruby bootstrap_form:
= bootstrap_form_for (@story) do |f|
%fieldset.actions.control-group
= f.text_field :title,hide_label: true,placeholder: "Title of your story",wrapper: { class: 'mb-3 story-title' },control_class: 'w-100 p-3'
= f.text_field :job_title,placeholder: "Job title",wrapper: { class: 'mb-3 job-title' },control_class: 'w-100 p-3'
= f.text_field :video_link,placeholder: "Attach video",wrapper: { class: 'mb-3 video-content hide' },control_class: 'w-100 p-3'
= f.rich_text_area :content,placeholder: "Write your story",wrapper: { class: 'rich-content' }
= f.text_field :tag_list,placeholder: "Insert job specific tags separated by comma",wrapper: { class: 'mb-3' },control_class: 'w-100 p-3'
= f.select :industry_id,options_for_select(Industry.all.map {|industry| [industry.name,industry.id]}),{},control_class: 'col-md-6 col-xs-12 p-3 input-Box'
= f.select :country_id,options_for_select(Country.all.map {|country| [country.name,country.id]}),control_class: 'col-md-6 col-xs-12 p-3 input-Box'
= f.submit "Submit",class: 'btn btn-action p3 mt-3'
这是我的控制器动作:
def create
@story = Story.new(story_params)
@story.user = current_user
if @story.save
@story.tag_list = tag_params
flash[:notice] = "You've successfully created a story titled #{@story.title}"
redirect_to root_path
else
flash[:error] = "Please fix the following error(s)"
end
end
当我将文件放到trix编辑器中时,据我了解,rails已将annex.js直接将图像上传到S3。当我从编辑器中删除文件时,就会出现问题。它不会在S3中删除关联的对象。仅当我删除整个故事时,该对象才会被删除。
我有两个选择和相关问题:
window.addEventListener("trix-file-accept",function(event) {
const acceptedTypes = ['image/jpeg','image/png']
if (!acceptedTypes.includes(event.file.type)) {
event.preventDefault()
alert("Only support attachment of jpeg or png files")
}
event.preventDefault()
var AWS = require('aws-sdk');
AWS.config.credentials = {
accessKeyId: gon.accessKey,secretAccessKey: gon.secretKey,region: 'us-west-1'
}
var s3 = new AWS.S3();
var params = { Bucket: gon.bucketName,Key: '#object-name#' };
var file = event.file
var upload = new AWS.S3.ManagedUpload({
params: {
Bucket: gon.bucketName,Key: file.name,Body: file,ACL: "public-read"
}
});
var promise = upload.promise();
promise.then(
function(data) {
alert("Successfully uploaded photo.");
},function(err) {
console.log(err)
return alert("There was an error uploading your photo: ",err.message);
}
);
})
-
此代码基本上绕过了Trix自动将图像上传到S3的操作。图像已成功上传到S3,但是问题在于富文本格式无法捕获它,这意味着故事。内容中将没有图像。 如果我走这条路线,如何将上传的图像与模型关联?
-
我在考虑的另一个选项是,我不确定该文件是否可能甚至绕过Trix在文件丢失时上传图像。而是在创建故事后仅将图像上传到S3。当我向用户添加个人资料照片时,就像我为用户控制器所做的事情:
def update
if current_user == @user
if @user.update(user_params)
if params[:avatar] != nil && params[:avatar] != "deleteAvatar"
@user.avatar.purge
@user.avatar.attach(params[:avatar])
elsif params[:avatar] == "deleteAvatar"
@user.avatar.purge
end
flash['notice'] = "Your profile was updated!"
render json: { status: true }
# redirect_to login_path
else
render json: { error: @user.errors.messages }
end
else
flash[:error] = "You are not authorized to do that"
redirect_to root_path
end
end
感谢社区的任何帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。