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

标签 ruby-on-rails ruby image file refile

我正在使用 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/

相关文章:

javascript - 我正在尝试使用 JavaScript 随机化 HTML 页面上的图像

ruby-on-rails - 为什么嵌入式 ruby​​ 不能在公用文件夹的静态页面中工作?

ruby-on-rails - any_instance should_receive 无法正常工作

ruby-on-rails - Rails 设计 : "You need to sign in or sign up before continuing" instead of "You will receive an email with instructions.."

ruby-on-rails - "Ruby on Rails-Tutorial"期间的路由错误

java - 如何压缩一批图像?

android - 如何在 EditText 上添加图像

ruby-on-rails - rails : Render view content in post-processor (model/helper issues)

ruby-on-rails - 通过 Ruby on Rails 中的 HTTP header 自定义授权

ruby - 使用迭代器将数组中的数字相乘