2016-04-27 132 views
0

我已經在部分this教程中指出,在使用Carrierwave上傳多個文件時刪除單個文件。刪除單個圖像可以正常工作,但在瀏覽器控制檯和Rails服務器輸出中出現錯誤。我的代碼:使用多個文件上傳時刪除單個文件Carrierwave

控制器:

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) 

回答

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 
相關問題