gpt4 book ai didi

ruby-on-rails - 重新归档 gem : multiple file uploads

转载 作者:数据小太阳 更新时间:2023-10-29 06:39:41 25 4
gpt4 key购买 nike

我正在使用 Refile 和 Rails 4。我正在按照他们的教程进行 multiple image upload .每个帖子可以有多个图像。我的模型看起来像这样:

后.rb:

has_many :images, dependent: :destroy
accepts_attachments_for :images, attachment: :file

图片.rb:

belongs_to :post
attachment :file

我可以上传文件,使用:

<%= f.attachment_field :images_files, multiple: true, direct: true, presigned: true %>

但是当我尝试检索像这样的图像时:

 <%= attachment_image_tag(@post.images, :file, :small) %>

我得到错误:

undefined method file for #<Image::ActiveRecord_Associations_CollectionProxy:0x007fbaf51e8ea0>

如何使用多张图片上传来检索带有 refile 的图片?

最佳答案

为了检索属于帖子的图像,您需要遍历图像数组

<% @post.images.each do |image| %>
<%= attachment_image_tag(image, :file, :fill, 300, 300) %>
<% end %>

助手 attachment_image_tag 采取:

  • [Refile::Attachment] 对象:类的实例有一个附件
  • [符号]名称:附件栏的名称

所以在这里,@posts.images 包含一个 image 对象数组。就是那个有附件的对象。

class Image < ActiveRecord::Base
belongs_to :post
attachment :file
end

然后,当您迭代 images 时,您将 image 对象 和附件列的名称提供给助手,此处为 :file .

关于ruby-on-rails - 重新归档 gem : multiple file uploads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com