使用多个文件上传Carrierwave时删除单个文件

时间:2016-04-27 00:38:58

标签: ruby-on-rails-4 carrierwave

我在部分this教程中介绍了在使用Carrierwave上传多个文件时删除单个文件。删除单个映像可以正常工作但我在浏览器控制台和Rails服务器输出中出现错误。我的代码:

控制器:

def destroy
  if params[:index]
    remove_image_at_index(params[:index].to_i)
    redirect_to :back
  else
    authorize @job
    @job.destroy
    respond_to do |format|
      format.html { redirect_to real_property_sales_url }
    end
  end
end

def remove_image_at_index(index)
  remain_images = @job.photos
  deleted_image = remain_images.delete_at(index)
  deleted_image.try(:remove!)
  @job.photos = remain_images
end

路线:

resources :real_property_sales

Link_to:

<%= link_to real_property_sale_path(@job, index: index), method: :delete, data: { confirm: 'Are you sure?' }, :remote => true do %>
  <span class="fa-stack fa-lg">
    <i class="fa fa-circle-o fa-stack-2x"></i>
    <i class="fa fa-times fa-stack-1x"></i>
  </span>
<% end %>

服务器输出:

Started DELETE "/real_property_sales/14?index=0" for 127.0.0.1 at 2016-04-27 10:18:41 +1000
Processing by RealPropertySalesController#destroy as JS
  Parameters: {"index"=>"0", "id"=>"14"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  RealPropertySale Load (0.3ms)  SELECT  "real_property_sales".* FROM "real_property_sales"  WHERE "real_property_sales"."id" = $1 LIMIT 1  [["id", 14]]
Redirected to http://localhost:3000/real_property_sales/14/edit
Completed 302 Found in 8ms (ActiveRecord: 0.8ms)


Started DELETE "/real_property_sales/14/edit" for 127.0.0.1 at 2016-04-27 10:18:41 +1000

ActionController::RoutingError (No route matches [DELETE] "/real_property_sales/14/edit"):

浏览器控制台:

DELETE http://localhost:3000/real_property_sales/14/edit 404 (Not Found)

1 个答案:

答案 0 :(得分:0)

我使用head :no_content

解决了这个问题
def destroy
  if params[:index]
    remove_image_at_index(params[:index].to_i)
    head :no_content
  else
    authorize @job
    @job.destroy
    respond_to do |format|
      format.html { redirect_to real_property_sales_url }
    end
  end
end