2016-04-27 2 views
0

Carrierwaveで複数のファイルアップロードを使用する場合、1つのファイルを削除するためのチュートリアルthisが続きました。単一のイメージの削除は正しく動作しますが、ブラウザコンソールと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 
関連する問題