Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 删除carrierwave的多个图像_Ruby On Rails_Carrierwave - Fatal编程技术网

Ruby on rails 删除carrierwave的多个图像

Ruby on rails 删除carrierwave的多个图像,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,我创建了一个示例Rails应用程序,它使用Carrierwave上传图像。我可以上传多张图片。但现在当我想删除它们时,我只是忙于寻找解决方案。我在文档中读到我们可以使用@user.remove\u avatar删除单个图像。但是如何删除多个图像呢?这并不是Rails最常用的方法,但是我有这个方法(我从其他地方的建议代码中修改了这个方法) 在我的视图中,输出我拥有的图像(其中我是图像位置): 这将删除具有多个图像的单个图像。这并不是Rails最常用的方法,但我有这个方法(我从其他地方的建议代码中修

我创建了一个示例Rails应用程序,它使用Carrierwave上传图像。我可以上传多张图片。但现在当我想删除它们时,我只是忙于寻找解决方案。我在文档中读到我们可以使用
@user.remove\u avatar
删除单个图像。但是如何删除多个图像呢?

这并不是Rails最常用的方法,但是我有这个方法(我从其他地方的建议代码中修改了这个方法)

在我的视图中,输出我拥有的图像(其中我是图像位置):


这将删除具有多个图像的单个图像。

这并不是Rails最常用的方法,但我有这个方法(我从其他地方的建议代码中修改了这个方法)

在我的视图中,输出我拥有的图像(其中我是图像位置):


这将删除具有多个图像的单个图像。

我同意楼上的答案,但当图像只剩下一个文件时,上述方法无效。我用下面的方法解决了这个问题


我同意楼上的答案,但当图像只剩下一个文件时,上述方法是无效的。我用下面的方法解决了这个问题


显示您的一些类似代码的模型associations@AniketShivamTiwari谢谢你考虑我的问题。但我并没有把它保存在不同的模型中。它作为json存储在一个模型中。我跟随多个图像上传。显示您的一些类似代码的模型associations@AniketShivamTiwari谢谢你考虑我的问题。但我并没有把它保存在不同的模型中。它作为json存储在一个模型中。我遵循了多个图像上载。如果您需要上载多个文件,我建议您创建一个直接存储文件的模型,因为它更灵活。如果您需要上载多个文件,我建议您创建一个直接存储文件的模型,因为它更灵活。
def delete_picture
  @i = params[:i].to_i

  remain_images = @project.avatars # copy initial avatars
  delete_image = remain_images.delete_at(@i) # delete the target image
  delete_image.try(:remove!) # delete image
  @project.avatars = remain_images # re-assign back
  @project.save  
end
 <%= link_to delete_picture_project_path(i: i), method: :delete, confirm: 'Delete?',       dataType: 'script', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
          <%= image_tag('delete') %>
        <% end %>
resources :projects do 
member do
    delete 'delete_picture', to: "projects#delete_picture"
    end
end
def delete_picture(index)
  remain_pictures = self.pictures
  if remain_pictures.size != 1
    delete_picture = remain_pictures.delete_at(index)
    delete_picture.remove!
    self.pictures = remain_pictures
  else
    remove_reports!
  end
  save
end

@user.delete_picture(params[:index].to_i)