我在Rails 4中使用Refile,我正在学习他们的multiple image upload教程。每个帖子可以有多个图像。我的模型是这样的:
Post.rb:
has_many :images, dependent: :destroy
accepts_attachments_for :images, attachment: :fileImage.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的镜像?
发布于 2015-07-05 17:42:31
为了检索属于帖子的图像,您需要迭代图像数组。
<% @post.images.each do |image| %>
<%= attachment_image_tag(image, :file, :fill, 300, 300) %>
<% end %>帮助器attachment_image_tag采用:
附件列的名称
所以在这里,@posts.images保存了一个image对象数组。它是具有附加文件对象。
class Image < ActiveRecord::Base
belongs_to :post
attachment :file
end然后,在迭代images时,将image object和附件列的名称(此处为:file )提供给帮助器。
发布于 2016-04-08 19:59:00
你是在主分支机构吗?
gem 'refile', require: "refile/rails", git: 'https://github.com/refile/refile.git', branch: 'master'https://stackoverflow.com/questions/31226156
复制相似问题