Reputation: 1287
I have a Property model which has many photos. Im trying to use the refile gem to upload images.
class Property < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_attachments_for :photos, attachment: :file
end
class Photo < ActiveRecord::Base
belongs_to :property
attachment :file
end
Here is the Photo section of schema.rb
create_table "photos", force: :cascade do |t|
t.integer "property_id"
t.string "file"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
And here is the relevant portion of the create new property form(slim)
.form
= form_for @property do |property|
.file_upload
= property.attachment_field :photos_files, multiple: true
= property.label :photos_files
= property.submit
Here is the properties controller
class PropertiesController < ApplicationController
def new
@property = Property.new
end
def create
@property = Property.new(property_params)
if @property.save!
redirect_to @property
else
render 'new'
end
end
private
def property_params
params.require(:property).permit(:attributes.... photos_files: [])
end
end
After submitting the form I get the following error.
NoMethodError (undefined method `file_id_will_change!' for #<Photo:0x007f96e8532560>):
After scratching my head for a while I can't see where Im messing up.
Upvotes: 3
Views: 461
Reputation: 1287
So after looking at the migration files in the included example application, I see that more model attributes are required. Coming from Carrierwave, I was under the impression that Refile was similar, only writing the file path to the database in a string column.
In this excerpt from the schema you can see that Refile stores the data differently.
create_table "documents", force: :cascade do |t|
t.integer "post_id", null: false
t.string "file_id", null: false
t.string "file_filename", null: false
t.string "file_size", null: false
t.string "file_content_type", null: false
end
After adding the new attributes, the uploader works well.
Upvotes: 3