title: Rails ajax: Failed to load resource: the server responded with a status of 404 (Not Found)
body: <p>I'm trying to rewrite some actions in my app with ajax.</p>
  
  <p>items_controller:</p>
  
  <pre><code>...
  def add_to_cart
    @cart = Cart.where(id: session[:cart_id]).first
    @cart = Cart.create if @cart.nil?
    session[:cart_id] = @cart.id
    session[:cart_items] = @cart.items
    session[:cart_items] &lt;&lt; @item
    redirect_to root_url
  end
  ...
  </code></pre>
  
  <p>items.js:</p>
  
  <pre><code>jQuery(function($) {
    $(".addtoCart").click( function() {
      var current_item = $(this).parents('tr')[0];
      if(confirm("Add to cart")) {
        $.ajax({
          url: '/items/' + $(current_item).attr('data-item-id') + '/add_to_cart',
          type: 'POST'
        });
      };
    });
  });
  </code></pre>
  
  <p>view file:</p>
  
  <pre><code>%tr{"data-item-id" =&gt; "#{i.id}"}
    %td
      %span.addtoCart Add to cart
  </code></pre>
  
  <p>routes:</p>
  
  <pre><code>Store::Application.routes.draw do
  
    root 'items#index'
  
    resources :items
  
    resources :items do
      get :add_to_cart, on: :member
    end
  
  end
  </code></pre>
  
  <p>When I click on <code>Add to cart</code> there is an error <code>Failed to load resource: the server responded with a status of 404 (Not Found)</code> and <code>POST http://localhost:3000/items/13/add_to_cart 404 (Not Found)</code> although <code>/items/13</code> is exists. Where did I made a mistake?</p>
  
  <p>error stacktrace:</p>
  
  <pre><code>Started POST "/items/13/add_to_cart" for 127.0.0.1 at 2014-03-30 02:55:03 +0400
  
  ActionController::RoutingError (No route matches [POST] "/items/13/add_to_cart")
  </code></pre>
  
  <p>Thanks!</p>