【发布时间】:2021-01-22 14:22:18
【问题描述】:
我正在向我的 Rails 6.0 网站添加一个博客。
我已安装 Action Text 并将 Active Storage 链接到 Cloudinary
然后我创建了一个带有照片的文章模型。
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
在 Articles/new.html.erb 上,我为“rich_body”添加了 Trix WYSIWYG 编辑器。
<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>
我设法创建了通过“照片”字段上传图片的博客文章。
现在我想在rich_body 中添加图像。我看着this article 这样做。 我用“附加文件”附加它们 (see screenshot of thsi button)。显示在编辑器中(see screenshot)
当我点击保存时,文章就创建好了。但只剩下“rich_body”的文本。没有附加图像。 如果我查看“参数”,则没有图像的痕迹。
=> <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>
知道我错过了什么吗?
非常感谢您的帮助。
格雷戈里
【问题讨论】:
标签: ruby-on-rails image attachment trix