13

I'm using Refile with Rails 4. I'm following their tutorial for multiple image upload. Each Post can have multiple Images. My models look like this:

Post.rb:

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

Image.rb:

belongs_to :post
attachment :file

I can upload files, fine by using:

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

but when I try to retrieve an image like:

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

I get the error:

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

How can I retrieve an image with refile using multiple image upload?

wurde
  • 2,487
  • 2
  • 20
  • 39
the_
  • 1,183
  • 2
  • 30
  • 61
  • Here is the source for attachment_image_tag: https://github.com/refile/refile/blob/master/lib/refile/rails/attachment_helper.rb#L39 – the_ Jul 05 '15 at 00:15
  • I also forgot to mention that if I do `@post.images.inspect`, I get an association with each object having the file nil, and the file_id set to the presigned, so I think that's part working fine. It's just when I try to view the image that it errors. – the_ Jul 05 '15 at 00:17
  • What is `@post` ? Is it a ***single record*** or a ***collection of records*** ? Please post the code for `@post`. – Pavan Jul 10 '15 at 11:44
  • @Pavan, @post is a single record. I did @post.inspect on show.html.erb and got: `# – Muhambi Jul 10 '15 at 20:47

2 Answers2

5

In order to retrieve images who belongs to a post, you need to iterate through the array of images.

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

The helper attachment_image_tag take:

  • [Refile::Attachment] object : Instance of a class which has an attached file.
  • [Symbol] name : The name of the attachment column

So here, @posts.images holds an array of image object. It's that object who has an attached file.

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

Then when you iterate images, you give to the helper the image object, and the name of the attachment column, here :file.

wurde
  • 2,487
  • 2
  • 20
  • 39
Florent Ferry
  • 1,397
  • 1
  • 10
  • 21
  • @Frorent Ferry, this doesn't work, I get the error: undefined method `file' for # – the_ Jul 05 '15 at 21:54
  • Have you a ´file_id:string´ on your Image migration ? Have you run ´rake db:migrate ´ ? – Florent Ferry Jul 06 '15 at 07:05
  • @Frorent Ferry, Yes, I have a file_id:string for my Image model. It shows in my schema file, so it definitely has been migrated. – the_ Jul 06 '15 at 18:09
  • So it works for me. Have you set `images_files` in strong params ? `params.require(:post).permit(..., images_files: [])` If yes, I can share my repo app used to write my response. – Florent Ferry Jul 06 '15 at 18:58
  • @frorent Ferry, I do have that in controller already. I would love to see the repo if you get a chance, thanks! – the_ Jul 09 '15 at 20:50
0

Are you on the master branch?

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