未定义的方法`images_will_change!'

时间:2018-05-27 13:04:18

标签: ruby-on-rails carrierwave

我是第一次发帖子,所以请忽略造型部分。 我正在使用Carrierwave进行多个文件上传。但我无法这样做。

仅限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

首先我得到未经许可的参数:: images 并且 未定义的方法`images_will_change!&#39;评论 任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

根据帖子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