无法再次删除carrierwave图像

时间:2014-09-18 14:47:05

标签: ruby ruby-on-rails-4 carrierwave

我似乎在使用carrierwave删除编辑表单中的图像时出现问题,我已经尝试了所有我认为应该做的事情。我希望能够在我的编辑表单中删除图像

<!-- File Upload -->
<%= f.fields_for :animal_images do |build| %>
  <div class="form-group">
  <% if build.object.image? %>

  <%= image_tag build.object.image_url :small_animal_image %>
  <%= build.check_box :remove_image, class: 'form-control' %>

<% else %>  
  <%= build.label :image, 'Upload Images' %><br />
  <%= build.file_field :image, class: 'form-control' %>
<% end %>
 </div>

 <%= build.link_to_remove "Remove this Image" %>

<% end %>

我也试过

<%= build.check_box :_destroy, class: 'form-control' %>

我已添加:remove_image和:_destroy到我允许的参数

并且在我的模型中也有正确的设置

class Animal < ActiveRecord::Base
  has_many :animal_images, dependent: :destroy
  accepts_nested_attributes_for :animal_images, allow_destroy: :true 
end

我错过了哪些明显的东西?

2 个答案:

答案 0 :(得分:0)

根据documentations

If you want to remove the file manually, you can call remove_avatar!, then save the object.
@user.remove_avatar!
@user.save
#=> true

所以这样的事情应该有效:@animal.remove_animal_images!

答案 1 :(得分:0)

希望这会对某人有所帮助,事实证明我在错误的地方有一个删除图像链接,这似乎导致复选框的点击不通过正确的参数,这就是工作设置的布局方式< / p>

<!-- File Upload -->
<%= f.fields_for :animal_images do |build| %>
<div class="form-group">
 <% if build.object.image? %>

  <%= image_tag build.object.image_url :small_animal_image %>
  <%= build.label "Remove Image" %>
  <%= build.check_box :_destroy, class: 'form-control' %>

<% else %>  
 <%= build.label :image, 'Upload Images' %><br />
 <%= build.file_field :image, class: 'form-control' %>
 <%= build.link_to_remove "Remove This Image" %>
<% end %>
</div>