gpt4 book ai didi

javascript - Basecamp 的 Trix WYSIWYG 编辑器 gem 不在 Rails 4 应用程序中保存文件附件

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:00 24 4
gpt4 key购买 nike

我添加了 Trix 编辑器 gem到我的 Rails 4 应用程序并完全按照该页面上的说明进行操作。保存帖子后文本显示正确(粗体/斜体/删除线/段落间距都很好),但是当我保存帖子时,我拖入文本编辑器的任何图像都消失了。我做错了什么?

谢谢。

app/assets/stylesheets/application.scss:

/*
*= require_self
*= require trix
*/

@import "bootstrap";
@import "bootstrap-sprockets";
@import "font-awesome";
@import url(https://fonts.googleapis.com/css?family=Delius+Swash+Caps);
@import url(https://fonts.googleapis.com/css?family=Reenie+Beanie);
@import url(https://fonts.googleapis.com/css?family=Special+Elite);
@import url(https://fonts.googleapis.com/css?family=Londrina+Shadow);

app/assets/javascripts/application.js:

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require bootstrap
//= require trix
//= require_tree .

_form.html.erb:

<%= form_for @article, html: {multipart: true} do |f| %>
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :subtitle %>
<%= f.text_field :subtitle %>
</p>

<%= f.label :text %>
<%= f.trix_editor :text, class: 'trix-content' %>

<p>
<%= f.label :tags %>
<%= f.text_field :tag_list %>
</p>

<p>
<%= f.submit %>
</p>

<% end %>

new.html.erb

<div class="container"> 
<div class="jumbotron">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
</div>
</div>
</div>
</div>

gem 文件:

gem 'rails', '4.2.2'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.6'
gem 'devise', '~> 3.5', '>= 3.5.6'
gem "font-awesome-rails"
gem 'paperclip', '~> 4.2'
gem 'aws-sdk', '~> 1.66'
gem 'figaro', '~> 1.1', '>= 1.1.1'
gem 'simple_form'
gem 'mail_form'
gem 'acts-as-taggable-on', '~> 3.4'
gem 'fog'
gem 'rmagick', '~> 2.15', '>= 2.15.4'
gem 'carrierwave'
gem "fog-aws"
gem 'trix', '~> 0.9.0'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

最佳答案

在此示例中,我创建了一个Image 模型和 Controller ,它们具有GETPOST 方法。我使用 refile 来处理文件上传,但它与 carrierwave 的工作方式类似。这个想法是监听事件 trix-attachment-add 并将 XHR POST 发送到您的 ImagesController。从这里,您可以将 JSON 响应发送回您的 XHR 请求并调用 JSON.parse 来捕获响应文本。 Trix 期望返回一个有效的 URL 并在附件上设置属性。在正确完成此操作之前,它将处于挂起状态并且不会实际保存图像和/或标题。

  # ImagesController
def create
@image = Image.new(image_params)
@image.save

respond_to do |format|
format.json { render :json => { url: Refile.attachment_url(@image, :image)} }
end
end

# Some Javascript

(function() {
var host, uploadAttachment;

document.addEventListener("trix-attachment-add", function(event) {
var attachment;
attachment = event.attachment;
if (attachment.file) {
return uploadAttachment(attachment);
}
});

host = "/images";

uploadAttachment = function(attachment) {
var file, form, xhr;
file = attachment.file;
form = new FormData;
form.append("Content-Type", file.type);
form.append("image[image]", file);
xhr = new XMLHttpRequest;
xhr.open("POST", host, true);
xhr.upload.onprogress = function(event) {
var progress;
progress = event.loaded / event.total * 100;
return attachment.setUploadProgress(progress);
};
xhr.onload = function() {
var href, url;
url = href = JSON.parse(this.responseText).url;
return attachment.setAttributes({
url: url,
href: href
});
};
return xhr.send(form);
};


}).call(this);

关于javascript - Basecamp 的 Trix WYSIWYG 编辑器 gem 不在 Rails 4 应用程序中保存文件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36917452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com