尝试使用carrierwave删除上传的图片
"images_attributes"=>{"0"=>{"remove_image"=>"0", "id"=>"13"}, "1"=>{"remove_image"=>"1", "id"=>"14"}, "2"=>{"remove_image"=>"0", "id"=>"15"}, "3"=>{"remove_image"=>"0", "id"=>"16"}, "4"=>{"remove_image"=>"0", "id"=>"17"}, "5"=>{"remove_image"=>"0", "id"=>"18"}}}
在控制器中获取此类参数
def update
@country = Country.find(params[:id])
if @country.update(country_params)
flash[:notice] = 'Country is successfully updated.'
redirect_to edit_admin_country_path
else
flash[:error] = @country.errors.full_messages[0]
render 'edit'
end
end
def country_params
permitted = [{images_attributes: ["image", "@original_filename", "@content_type", "@headers", "_destroy", "id", "remove_image"]}]
params.require(:country).permit(*permitted)
end
class Country < ActiveRecord::Base
has_many :images
....
end
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :country
end
但是当用这些参数更新对象时没有任何反应,我错过了什么?
更新
VARBINARY
答案 0 :(得分:3)
您的表单看起来不错,但您错过了控制器操作
我看起来像是:class ImageController < ApplicationController
...
def update
@image = Image.find(params[:id])
...
if params[:images][:remove_image].present?
@image.remove_image!
end
@image.save
end
end
如果您想手动删除文件,可以调用remove_avatar !,然后保存对象。