繁体   English   中英

Refile gem:多个文件上传

[英]Refile gem : multiple file uploads

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

Post.rb:

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

Image.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>

如何使用多个图像上传来重新检索图像?

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

<% @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 object ,以及附件列的名称,此处为:file

你在主分公司吗?

gem 'refile', require: "refile/rails", git: 'https://github.com/refile/refile.git', branch: 'master'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM