【发布时间】:2019-12-24 16:30:57
【问题描述】:
我遵循了有关如何使用活动记录上传多个图像的教程。我已经安装了活动记录,将我的关联添加到我的模型中,当我创建服务(我的模型)时,当我上传 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>
我做 Service.last.images 时的 Rails c
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
我希望我的 3 张图片显示在 FE 上
【问题讨论】:
-
当您在控制台中执行
Service.last.images.count时,您会得到 3 吗? -
你好@MZaragoza 是的,我愿意!我在前端所能看到的只是损坏的图像和来自控制台的活动记录字符串。
-
在 ERB 文件上,如果您执行 ` img
-
我在前端得到这个
img img img [#<ActiveStorage::Attachment id: 16, name: "images", record_type: "Service", record_id: 15, blob_id: 16, created_at: "2019-08-19 18:56:12">, #<ActiveStorage::Attachment id: 17, name: "images", record_type: "Service", record_id: 15, blob_id: 17, created_at: "2019-08-19 18:56:13">, #<ActiveStorage::Attachment id: 18, name: "images", record_type: "Service", record_id: 15, blob_id: 18, created_at: "2019-08-19 18:56:13">]
标签: ruby ruby-on-rails-3 activerecord