简体   繁体   English

动作文本(Trix):无法在 Rails 上以富文本添加图像(“附加文件”未保存)

[英]Action text (Trix) : cannot add images in rich text on Rails (“attach files” not saved)

I am adding a blog to my Rails 6.0 website.我正在向我的 Rails 6.0 网站添加一个博客。

I have installed Action Text and linked Active Storage to Cloudinary我已安装Action Text并将 Active Storage 链接到 Cloudinary

Then I created a Article model which has a photo.然后我创建了一个带有照片的文章 model。

class Article < ApplicationRecord   
  has_one_attached :photo
  has_rich_text :rich_body
  validates :title, uniqueness: true
  validates :lead, presence: true
  validates :rich_body, presence: true 
end

On Articles/new.html.erb I to added the Trix WYSIWYG editor for "rich_body".在 Articles/new.html.erb 上,我为“rich_body”添加了 Trix 所见即所得编辑器。

<div class="container">
  <%= simple_form_for(@article) do |f| %>
    <div class="form-inputs">
      <%= f.input :title %>
      <%= f.input :lead %>
      <%= f.input :photo, as: :file %>
      <%= f.input :caption %>
    </div>
    <div class="form-inputs">
      <%= f.rich_text_area :rich_body %>
    </div>
    <div class="form-actions">
      <%= f.button :submit, class: "btn btn-primary" %>
    </div>
  <% end %>
</div>

I managed to create blog articles in which the image was uploaded through the "Photo" field.我设法创建了通过“照片”字段上传图像的博客文章。

Now I want to add images within the rich_body.现在我想在rich_body 中添加图像。 I looked at this article to do so.我看这篇文章是为了这样做。 I attach them with the "Attach Files" ( see screenshot of thsi button ).我用“附加文件”附加它们(参见 thsi 按钮的屏幕截图)。 It is displayed in the editor ( see screenshot )它显示在编辑器中(见截图

When I hit save, the article is created.当我点击保存时,文章就创建好了。 But only the text of the "rich_body" remains.但只剩下“rich_body”的文本。 There is no attached image.没有附加图像。 If I look at "params", there is no trace of the image.如果我查看“参数”,则没有图像的痕迹。

=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"DDGTxfT/aSoY5I5FNMrzv+hJ+seNlce0sJufFBREfUBlETeLGjejbnoxq2jhyzwGcZe1UAbCJbozi1O4vH5GyQ==", "article"=><ActionController::Parameters {"title"=>"Post title", "lead"=>"Summary or the post", "photo"=>#<ActionDispatch::Http::UploadedFile:0x00008b18957b5az8 @tempfile=#<Tempfile:/tmp/RackMultipart20210122-9530-1nu0w1w.jpg>, @original_filename="tools.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[photo]\"; filename=\"tools.jpg\"\r\nContent-Type: image/jpeg\r\n">, "caption"=>"This is the caption of the photo", "rich_body"=>"<div>Some text. The attached image is below.<br><br>The attached image is above<br><br></div>"} permitted: false>, "commit"=>"Créer un(e) Article", "controller"=>"articles", "action"=>"create"} permitted: false>

Any idea of what I am missing?知道我缺少什么吗?

Many thanks in advance for your help.非常感谢您的帮助。

Gregory格雷戈里

Are you using ActiveStorage ?你在使用ActiveStorage吗?

Should have ran commands to create the necessary tables:应该运行命令来创建必要的表:

bundle exec rails active_storage:install
bundle exec rails db:migrate

Check to ensure you have the two tables active_storage_blobs and active_storage_attachments .检查以确保您有两个表active_storage_blobsactive_storage_attachments

I know this is old but there was an issue similar to this that might have to do with CORS on the S3 bucket.我知道这很旧,但有一个与此类似的问题,可能与 S3 存储桶上的 CORS 有关。 https://github.com/rails/rails/issues/36982#issuecomment-636330624 https://github.com/rails/rails/issues/36982#issuecomment-636330624

I'm running into the same issue.我遇到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM