gpt4 book ai didi

ruby-on-rails-4 - 删除上传的图像 rails 4 carrierwave

转载 作者:行者123 更新时间:2023-12-04 00:35:06 25 4
gpt4 key购买 nike

尝试使用carrierwave删除上传的图片

<%= f.fields_for :images do |ff| %>
<div class="form-group">
<label>
<%= ff.check_box :remove_image %>
<%= image_tag ff.object.image %>
</label>
</div>
<% end %>

在 Controller 中获取此类参数

"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

最佳答案

您的表单看起来不错,但您缺少 Controller 操作

我的样子:

class ImageController < ApplicationController
...
def update
@image = Image.find(params[:id])
...
if params[:images][:remove_image].present?
@image.remove_image!
end
@image.save
end
end

If you want to remove the file manually, you can call remove_avatar!, then save the object.

关于ruby-on-rails-4 - 删除上传的图像 rails 4 carrierwave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44651969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com