我有一个包含很多照片的 Property 模型。我正在尝试使用 refile gem上传图片。
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
这是 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
这是创建新属性表单( slim )的相关部分
.form
= form_for @property do |property|
.file_upload
= property.attachment_field :photos_files, multiple: true
= property.label :photos_files
= property.submit
这是属性 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
提交表单后出现以下错误。
NoMethodError (undefined method `file_id_will_change!' for #<Photo:0x007f96e8532560>):
挠了挠脑袋,我看不出我哪里搞砸了。
最佳答案
因此,在查看包含的示例应用程序中的迁移文件后,我发现需要更多的模型属性。来自 Carrierwave,我的印象是 Refile 类似,只是在字符串列中写入数据库的文件路径。
在这个模式的摘录中,您可以看到 Refile 以不同的方式存储数据。
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
添加新属性后, uploader 运行良好。
关于ruby-on-rails - Rails - Refile Gem - 未定义的方法 `file_id_will_change!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344754/