1

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"]
Community
  • 1
  • 1
Kevin Brown
  • 12,602
  • 34
  • 95
  • 155
  • Please include the source code for the models and the controllers for better understanding. – Dharam Gollapudi Sep 02 '16 at 22:23
  • @Dharam, does that help? It's pretty straight forward. Based on the links I provided, my issue is same. – Kevin Brown Sep 02 '16 at 22:48
  • Without knowing how your database is set up.... You have has_many :attachments. So in your params you should need something like params.require(:job_posting).permit(:title, document_attributes: [:filename, :id] – DottedT Sep 05 '16 at 01:42
  • @DottedT what if the array is hashes? I thought declaring an empty array allowed all fields. I've also tried using '.permit!' Which hasn't worked. – Kevin Brown Sep 05 '16 at 01:44
  • Declaring an empty array in require defeats the purpose of it. Require is there expressly to make you be specific about what is and is not allowed. As for "what if the array is hashes", Parameters are sent as a hash so I'm not sure what you mean. – DottedT Sep 05 '16 at 01:56
  • @DottedT, sorry I realize the hashes part was poorly phrased. The array for `document_attributes` is an array of hashes (since we're inputting multiple attributes). Can you confirm this works? I know it _should_, but I think I've tried this. I'll confirm as soon as I can. – Kevin Brown Sep 05 '16 at 02:32
  • @DottedT, just tried your recommendation (which I agree, makes sense), but explicitly calling `:id, :filename` prevents Refile from saving anything at all. – Kevin Brown Sep 05 '16 at 02:47
  • What I gave was just an example as I'm just guessing on what you're trying to save to the database for documents and/or story or attachments. I'm also guessing at how the form is constructed. The point is that it seems you're trying to use a controller to save to a has_many related model. You'll also need to use accepts_nested_attributes_for. Have a look at http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – DottedT Sep 05 '16 at 04:31
  • Could you provide the `attachments` table schema? – Dharam Gollapudi Sep 05 '16 at 07:42
  • @Dharam, updated. – Kevin Brown Sep 05 '16 at 13:32

1 Answers1

1

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.

Dharam Gollapudi
  • 6,328
  • 2
  • 34
  • 42