Search code examples
ruby-on-railsfile-uploadnested-attributesruby-on-rails-5refile

Refile Metadata for Multiple Attachments


Using the refile gem, I want to save filenames for multiple uploads. I've opened an issue on github here, but want to reach out to SO also. It's actually noted here on SO also.

Following the readme, I've applied the correct strong_params, I can see they're permitted in the log, but the don't write to db.

A :story accepts_attachments_for :documents, and I have my metadata set up correctly for Documents class.

When I submit, nothing is rejected--I can see in the log that params are accepted, but the metadata (filename, etc) is not saved.

Where do I start?

Source:

class Attachment < ApplicationRecord
  attachment :file
end

class Problem < ApplicationRecord
  has_many :attachments, dependent: :destroy
  accepts_attachments_for :attachments, append: true, attachment: :file
end

and controller params:

def mp_problem_params
  params.require(:problem).permit(:title, attachments_files: [])
end

My Attachment.rb columns:

=> ["id",
 "attachment_filename",
 "knowledge_id",
 "attachment_size",
 "content_type",
 "created_at",
 "updated_at",
 "file_id"]

Solution

  • Based on refile documentation, metadata is stored on the object/model that you have setup to have attachment. In your case, Attachment model. It also expects certain columns to be present in order to store the metadata. Based on your attachment model:

    class Attachment < ApplicationRecord
      attachment :file
    end
    

    Then you have to have the following columns present in the attachments table:

    file_filename
    file_size
    file_content_type
    

    If you add those columns to the attachments table then the metadata of the file will get stored.