未定义的方法 `images_will_change!'

undefined method `images_will_change!'

我是第一次发帖,所以请忽略样式部分。 我正在为多个文件使用 Carrierwave uploads.But 我无法这样做。

API 仅

Controller code:

def create
@comment = Comment.new(comment_params)
@comment.save
end

def comment_params
params.require(:comment).permit(:description, {images: []})
end
------------
Comment.rb

class Comment < ApplicationRecord
 mount_uploaders :images, FileUploader
end
------------
Migration file

class AddImagesToComments < ActiveRecord::Migration[5.1]
 def change
  add_column :comments, :images, :string, array: true, default: []
 end
end

首先我得到 Unpermitted parameter: :images 并且 未定义的方法 `images_will_change!'征求意见 谁能帮我解决这个问题。

根据post Multiple Images Uploading With CarrierWave and PostgreSQL Array。您需要在控制器中单独更新图像。

before_action :set_comment

def create
  add_more_images(images_params[:images])
  flash[:error] = "Failed uploading images" unless @comment.save
  redirect_to :back
end

private
def set_comment
  @comment = Comment.where(id: params[:id]).first || Comment.new
end

def add_more_images(new_images)
  images = @comment.images 
  images += new_images
  @comment.images = images
end

def images_params
  params.require(:comment).permit({images: []}) # allow nested params as array
end