Fernando Maymone
Fernando Maymone

Reputation: 355

Saving multiple files with refile not working

Im using refile to upload some photos for an album in my project.

My models are like this:

class Album < ApplicationRecord
  has_many :photos,dependent: :destroy

class Photo < ApplicationRecord
  belongs_to :user
  belongs_to :album
  attachment :media

Im trying to create a view that the user can upload multiple files to a Album. My view is like this:

        <%= form_for @album, method: :put do |f| %>
          <%= f.label :name %>
          <%= f.text_field :name %>
          <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">                            
                            <span class="btn btn-default btn-file">
                                <i class="fa fa-cloud-upload fa-lg"></i> Upload Photos 
                                <%= f.attachment_field :photos, multiple: true %>
                            </span>
                        </div>      
                    </div>
                </div>
          <div id="photos"><%= render 'photos/list' %></div>
          <div class="actions">
            <%= f.submit 'Save Album', class: 'btn btn-success' %>
          </div>
        <% end %>

But im getting this error:

NoMethodError in Albums#edit
Showing /home/ubuntu/workspace/app/views/albums/_form.html.erb where line #19 raised:

undefined method `photos_attachment_definition' for #<Album:0x007fd014d3d9e8>

This is driving me crazy. Someone have a light on it?

Thanks

Upvotes: 2

Views: 47

Answers (1)

Fernando Maymone
Fernando Maymone

Reputation: 355

The problem here is that my model should be:

class Album < ApplicationRecord

  belongs_to :user
  has_many :photos,dependent: :destroy

  accepts_attachments_for :photos, attachment: :media

And my view:

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

Now is working

Upvotes: 1

Related Questions