2016-05-26 3 views
2

rails ajaxでテーブル行を削除しようとしています。しかし、私は私のコンソールで以下のエラーを取得しています。ajaxとレールでテーブルレコードを削除する際に404エラーが見つかりません

DELETE http://localhost:3000/spares 404 (Not Found) 

以下は私のコードブロックです。

<%= link_to 'Destroy', spare, method: :delete, data: { confirm: "Are you sure you want to delete the #{spare.name}?" }, class: "btn btn-danger btn-xs",remote: true %></td> 

destroy.js.erbファイル

//$('tr #<%= @spare.id %>').slideUp(); 
var element = document.getElementById(<%= @spare.id %>); 
alert(element); 
alert(hello); 
element.parentNode.removeChild(element); 

**上記のコードでアラートも表示されません。

<% @spares.each_with_index do |spare, index| %> 
     <tr id="li_<%= spare.id %>"> 
     <td><%= index + 1 %></td> 
     <td><%= spare.name %></td> 

     <!-- authorization check --> 
     <% if can? :show, Spare %> 
      <td><%= link_to 'Show', spare, class: "btn btn-warning btn-xs" %> 
     <% end %> 

     <!-- authorization check --> 
     <% if can? :edit, Spare %> 
      <%= link_to 'Edit', edit_spare_path(spare), class: "btn btn-primary btn-xs" %> 
     <% end %> 

     <!-- authorization check --> 
     <% if can? :destroy, Spare %> 
      <%= link_to 'Destroy', spare, method: :delete, data: { confirm: "Are you sure you want to delete the #{spare.name}?" }, class: "btn btn-danger btn-xs",remote: true %></td> 

私はレールパネルでレンダリングされているフォーマットをチェックし、それはJSを示しています**

以下は私のテーブルです。しかしレンダリングではdestroy.js.erbが表示されません。以下は

サーバログ

"/spares" for 127.0.0.1 at 2016-05-26 09:02:29 +0530 
    ActionController::RoutingError (No route matches [DELETE] "/spares"): 
+0

「<%= @ spare.id%>」はおそらく文字列ではなく整数を返します。 '<%= @ spare.id.to_s%>'を試してください。 –

+0

申し訳ありませんが、それは助けませんでした。警告も表示されないので、destroy.js.erbファイル自体はレンダリングされません。 –

+0

私はサーバログエラー –

答えて

0

に誤りであるこれを実行してください。

index.html.erb

<% @spares.each_with_index do |spare, index| %> 
     <tr id="li_<%= spare.id %>"> 
     <td><%= index + 1 %></td> 
     <td><%= spare.name %></td> 

     <!-- authorization check --> 
     <% if can? :show, Spare %> 
     <td><%= link_to 'Show', spare, class: "btn btn-warning btn-xs" %></td> 
     <% end %> 

     <!-- authorization check --> 
     <% if can? :edit, Spare %> 
     <td><%= link_to 'Edit', edit_spare_path(spare), class: "btn btn-primary btn-xs" %> </td> 
     <% end %> 

     <!-- authorization check --> 
     <% if can? :destroy, Spare %> 

     <td><%= link_to 'Destroy', spare, method: :delete, data: { confirm: "Are you sure you want to delete the #{spare.name}?" }, class: "btn btn-danger btn-xs",remote: true %></td> 
     <% end %> 
     </tr> 
    <% end %> 

spares_controller.rb

def destroy 
    @spare.destroy 
    respond_to do |format| 
     format.html { redirect_to spares_url, notice: 'Spare was successfully destroyed.' } 
     format.js 
    end 
    end 

destroy.js.erb

$("#li_<%= @spare.id %>").remove() 

あなたが前に、あなたのコントローラ内のフィルタの前にset_spareを持っていることを確認してくださいアクションを破壊する。 このように

def set_spare 
    @spare = Spare.find(params[:id]) 
end 
+0

こんにちは、変更はdestroy.js.erbファイルにのみありますか? 。私はこれらの変更を行ったが、同じエラーがまだ起きている –

+0

trとtdのように、tgasを適切に閉じていますか?上記の貼り付けたスニペットがhtmlタグを正しく閉じるのではなく、私はscretchからアプリケーションをビルドします。あなたが言うなら、アプリケーションコード全体を共有してください。 –

+0

もしあなたがそれを共有できるなら、大きな助けになるでしょう:) –

関連する問題