ruby - 使用活动存储上传多张图片时未显示图片
问题描述
我遵循了有关如何使用活动记录上传多个图像的教程。我已经安装了活动记录,将我的关联添加到我的模型中,当我创建服务(我的模型)时,当我上传 3 张图像时,一切似乎都在工作,即没有活动记录错误但是我在 FE 上看不到我的图像,我看到的都是是一个破碎的图像。
当我进入 Rails c 并输入 Service.images.last 时,我可以看到 images 为 nil。有人知道为什么吗?
你还有什么需要知道或从我这里看到的吗?
谢谢
我显示图像的代码:
<%=@service.images.each do |img| %>
<%= cl_image_tag @service.images, crop: :fill, class: 'card-image', class: 'card-image-show'%>
<%end %>
导轨 c
Service Load (59.6ms) SELECT "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007fffc3ff08a0 @name="images", @record=#<Service id: 14, name: "Test Carpet cleaning", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 12:32:35", updated_at: "2019-08-19 12:32:35", photo: nil, images: nil>, @dependent=:purge_later>
Rails c 当我做 Service.last.images
irb(main):013:0> Service.last.images
Service Load (0.6ms) SELECT "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007f92fd1f8780 @name="images", @record=#<Service id: 15, name: "test multiple images", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 18:56:12", updated_at: "2019-08-19 18:56:13", photo: nil, images: nil>, @dependent=:purge_later>
服务模式
class Service < ApplicationRecord
has_many_attached :images
mount_uploader :photo, PhotoUploader
end
我想在 FE 上显示我的 3 张图片
解决方案
这是我最终用来让图像显示在我的 SHOW PAGE 上的代码。
<% if @service.images.attached? %>
<p>
<strong>Images:</strong>
<br>
<% @service.images.each do |image| %>
<%= image_tag(image) %>
<% end %>
</p>
<% end %>
我cl_image_tag
用来显示我的图像导致图像显示损坏。我应该用上面的。
资料来源:https ://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond
推荐阅读
- datatables - 数据表中的多个请求和响应调用
- android - 尝试在后台运行应用程序(最小化/屏幕锁定/从正在运行的应用程序中删除)
- sql - 如何缩短多次使用相同表达式的 postgresql 查询?
- mosaic-decisions - 在 Mosaic 决策的 sql 节点中使用 current_time 系统参数
- python - Python:只有您可以访问时从谷歌驱动器下载文件
- xamarin.forms - Xamarin iOS - 列表视图中标签中的多种文本颜色
- qt - Qt with OpenCV:程序意外完成
- python - 根据行值向 pandas df 添加多列
- javascript - 如何让这个 nodejs lambda 等到工作完成?
- javascript - 使用 Buffer.alloc 在 nodejs 中进行 base64 转换的问题