【问题标题】:Rails - Refile Gem - undefined method `file_id_will_change!'Rails - Refile Gem - 未定义的方法`file_id_will_change!
【发布时间】:2015-12-17 21:40:39
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails slim-lang refile


    【解决方案1】:

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

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

    【讨论】:

    • 那么通过迁移添加它们是这里唯一必要的事情吗?我会试试的
    • @malditojavi - 我删除了我正在使用的模型,并创建了一个与上述属性匹配的新模型。对我来说,这就是我所需要的。
    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2012-10-28
    • 2016-08-27
    • 2016-06-03
    相关资源
    最近更新 更多