ruby-on-rails - Rails - Refile Gem - 未定义的方法 `file_id_will_change!'

标签 ruby-on-rails slim-lang refile

我有一个包含很多照片的 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/

相关文章:

html - Dashing 仪表板中的第一行未对齐

javascript block 中的 ruby​​ [slim 模板]

ruby-on-rails - 使用 File gem 上传的文件的 Pundit 授权

ruby-on-rails - 重新提交 accepts_attachments_for issue

ruby-on-rails - 我可以在 Refile 初始化程序中为 Rails 4 项目中的不同功能配置单独的 S3 存储桶吗?

ruby-on-rails - Cucumber: stub 、模拟或记录客户调用(谷歌地图)

ruby-on-rails - Michael Hartl 的 Rails 教程 : Rspec's "Expected response to be a redirect to ..." is wrong

ruby-on-rails - 新手问题,这里的 'yield'是什么意思?

ruby-on-rails-3.2 - Rails + Slim 源输出