3

我有一个有很多照片的属性模型。我正在尝试使用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

这是属性控制器

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>):

挠了一阵头后,我看不出我在哪里搞砸了。

4

1 回答 1

3

因此,在查看了包含的示例应用程序中的迁移文件后,我发现需要更多模型属性。来自 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

添加新属性后,上传器运行良好。

于 2015-12-18T00:25:37.103 回答