Refile gem : multiple file uploads
Asked Answered
B

2

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?

Bearce answered 5/7, 2015 at 0:12 Comment(4)
Here is the source for attachment_image_tag: github.com/refile/refile/blob/master/lib/refile/rails/…Bearce
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.Bearce
What is @post ? Is it a single record or a collection of records ? Please post the code for @post.Edgewise
@Pavan, @post is a single record. I did @post.inspect on show.html.erb and got: #<Post id: 2, name: "RefileTest", created_at: "2015-07-04 23:54:00", updated_at: "2015-07-04 23:54:00"Henceforward
W
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.

Woodbine answered 5/7, 2015 at 9:42 Comment(5)
@Frorent Ferry, this doesn't work, I get the error: undefined method `file' for #<Image::ActiveRecord_Associations_CollectionProxy:0x007f8d1adb7b90>Bearce
Have you a ´file_id:string´ on your Image migration ? Have you run ´rake db:migrate ´ ?Woodbine
@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.Bearce
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.Woodbine
@frorent Ferry, I do have that in controller already. I would love to see the repo if you get a chance, thanks!Bearce
P
0

Are you on the master branch?

gem 'refile', require: "refile/rails", git: 'https://github.com/refile/refile.git', branch: 'master'
Pamphleteer answered 8/4, 2016 at 11:58 Comment(1)
This should be commentSeldun

© 2022 - 2024 — McMap. All rights reserved.