我要添加一个博客到我的Rails 6.0网站。
我已经安装了Action Text并将Active Storage链接到cloudary
然后我创建了一个有照片的Article模型。
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
/new.html文章。为"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>
我设法创建了通过"照片"上传图片的博客文章。场。
现在我想在rich_body中添加图像。我看了这篇文章。我用"附加文件"将它们附加起来。(见此按钮的截图)。它显示在编辑器中(见截图)
当我点击保存时,文章就创建了。但只有"rich_body"的文本;仍然存在。没有附件图片。如果我看"params",没有图像的痕迹。
=> <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"rnContent-Type: image/jpegrn">, "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>
你知道我错过了什么吗?
提前感谢您的帮助。
格里高利
您是否使用ActiveStorage
?
应该运行命令来创建必要的表:
bundle exec rails active_storage:install
bundle exec rails db:migrate
检查是否有两个表active_storage_blobs
和active_storage_attachments
。
我知道这是旧的,但有一个类似的问题,可能与S3桶上的CORS有关。https://github.com/rails/rails/issues/36982 issuecomment - 636330624
我也遇到了同样的问题。
为了解决这个问题,我必须更新blob视图
# app/views/active_storage/blobs/_blob.html.erb
--- <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
+++ <% if local_assigns[:in_gallery] %>
+++ <%= cl_image_tag blob.key, width: 800, height: 600, c_limit: true %>
+++ <% else %>
+++ <%= cl_image_tag blob.key, width: 1024, height: 768, c_limit: true %>
+++ <% end %>
这样做,图像,指向一个本地url指向cloudary