title: Rails suddenly gives routes error when using form with ajax
body: <p>I've made a button which when clicked creates a <code>shopping_cart_item</code> and then redirects to the same page showing a button to delete this same <code>shopping_cart_item</code>. This al works fine. When I try to use ajax the remove from cart button works fine but all of a sudden when I click the add to cart button I get the following error:</p>
  
  <pre><code>Started POST "/shopping_cart_items" for 127.0.0.1 at 2014-01-26 17:04:04 +0100
  Processing by ListsController#create as JS
    Parameters: {"utf8"=&gt;"✓", "course_id"=&gt;"10", "listtype"=&gt;"shopping_cart_items"}
    User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '7c8d45b38c4739022112db6ec137f31f60fada60' LIMIT 1
     (0.2ms)  BEGIN
    SQL (0.8ms)  INSERT INTO "shopping_cart_items" ("course_id", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id"  [["course_id", 10], ["created_at", Sun, 26 Jan 2014 16:04:04 UTC +00:00], ["updated_at", Sun, 26 Jan 2014 16:04:04 UTC +00:00], ["user_id", 1]]
     (0.7ms)  COMMIT
    Rendered browse_courses/_uncart.html.erb (2.1ms)
    Rendered lists/create.js.erb (3.9ms)
  Completed 500 Internal Server Error in 15ms
  
  ActionView::Template::Error (No route matches {:action=&gt;"destroy", :listtype=&gt;"shopping_cart_items", :controller=&gt;"lists"}):
      1: &lt;%= form_tag({:controller =&gt; 'lists', :action =&gt; 'destroy', listtype: 'shopping_cart_items'}, method: 'delete', remote: true) do %&gt;
      2:   &lt;div&gt;&lt;%= hidden_field_tag :course_id, params[:id] %&gt;&lt;/div&gt;
      3: 
      4:   &lt;%= button_tag(name: nil, class: 'btn btn-primary') do %&gt;
    app/views/browse_courses/_uncart.html.erb:1:in `_app_views_browse_courses__uncart_html_erb__2389702037921073695_70286519726020'
    app/views/lists/create.js.erb:1:in `_app_views_lists_create_js_erb___3945578670069953364_70286487293260'
    app/controllers/lists_controller.rb:6:in `create'
  
  
    Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
    Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
    Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (15.6ms)
  </code></pre>
  
  <p>The form used to add an item to the cart</p>
  
  <pre><code>&lt;%= form_tag({:controller =&gt; 'lists', :action =&gt; 'create', listtype: 'shopping_cart_items'}, method: 'post', remote: true) do %&gt;
    &lt;div&gt;&lt;%= hidden_field_tag :course_id, params[:id] %&gt;&lt;/div&gt;
  
    &lt;%= button_tag(name: nil, class: 'btn btn-primary') do %&gt;
      add to cart
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>the form used to remove an item from the cart</p>
  
  <pre><code>&lt;%= form_tag({:controller =&gt; 'lists', :action =&gt; 'destroy', listtype: 'shopping_cart_items'}, method: 'delete', remote: true) do %&gt;
    &lt;div&gt;&lt;%= hidden_field_tag :course_id, params[:id] %&gt;&lt;/div&gt;
  
    &lt;%= button_tag(name: nil, class: 'btn btn-primary') do %&gt;
          deletings
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My routes for the lists controller:</p>
  
  <pre><code> listtype_regexp = Regexp.new([:shopping_cart_items, :currently_taking_items, :recently_viewed_items].join("|"))
   resources :lists, only: [:create, :destroy], path: ':listtype', constraints: { listtype: listtype_regexp }
  </code></pre>
  
  <p>the lists controller:</p>
  
  <pre><code>class ListsController &lt; ApplicationController
  
  
    def create
      addToList!(params[:course_id], current_user.shopping_cart_items)
      respond_to do |format|
        format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
        format.js
      end
    end
  
    def destroy
      removeFromList!(params[:course_id], current_user.shopping_cart_items)
      respond_to do |format|
        format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
        format.js
      end
    end
  end
  </code></pre>
  
  <p>create.js.erb</p>
  
  <pre><code>$("#cart_form").html("&lt;%= escape_javascript(render('browse_courses/uncart')) %&gt;")
  </code></pre>
  
  <p>destroy.js.erb</p>
  
  <pre><code>$("#cart_form").html("&lt;%= escape_javascript(render('browse_courses/cart')) %&gt;")
  </code></pre>
  
  <p>rake routes:</p>
  
  <pre><code>        Prefix Verb   URI Pattern              Controller#Action
           users GET    /users(.:format)         users#index
                 POST   /users(.:format)         users#create
        new_user GET    /users/new(.:format)     users#new
            user GET    /users/:id(.:format)     users#show
                 PATCH  /users/:id(.:format)     users#update
                 PUT    /users/:id(.:format)     users#update
                 DELETE /users/:id(.:format)     users#destroy
        sessions POST   /sessions(.:format)      sessions#create
     new_session GET    /sessions/new(.:format)  sessions#new
         session DELETE /sessions/:id(.:format)  sessions#destroy
           lists POST   /:listtype(.:format)     lists#create {:listtype=&gt;/shopping_cart_items|currently_taking_items|recently_viewed_items/}
            list DELETE /:listtype/:id(.:format) lists#destroy {:listtype=&gt;/shopping_cart_items|currently_taking_items|recently_viewed_items/}
  browse_courses GET    /:type(.:format)         browse_courses#index {:type=&gt;/courses|institutes|programmes|staff/}
   browse_course GET    /:type/:id(.:format)     browse_courses#show {:type=&gt;/courses|institutes|programmes|staff/}
            root GET    /                        static_pages#home
          signup GET    /signup(.:format)        users#new
          signin GET    /signin(.:format)        sessions#new
         signout DELETE /signout(.:format)       sessions#destroy
            help GET    /help(.:format)          static_pages#help
           about GET    /about(.:format)         static_pages#about
         contact GET    /contact(.:format)       static_pages#contact
  </code></pre>