Rails no route matches PUT, but rake route shows it exists

Viewed 588

I'm trying to implement a page where I can add a quantity to a product in a cart. Any good tutorials on this?

So I tried implementing the typical edit form with the form_for but that kept giving me errors like this...

<%= form_for(@cart[0]) do |f| %>
  <% if @cart.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@cart.errors.count, "error") %> prohibited this cart from being saved:</h2>

      <ul>
      <% @cart.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And the error:

No route matches {:action=>"show", :controller=>"carts", :id=>nil} missing required keys: [:id]

...keep in mind this is in the edit action, so I don't know why it is complaining about the show action. Anyways I have made id: false and customized my primary key to cart_id

In the index.html file I customize parameters

<td><%= link_to 'Edit', edit_cart_path(cart, :id => cart.cart_id, :product_id => cart.product_id) %></td>

This is what I have in the controller action

  def edit
    @cart = Cart.where(:cart_id => params[:id], :product_id => params[:product_id])
  end

And I have confirmed it finds the correct record.

So, I said screw all of this and decided to just make a form_tag and then send the quantity as a parameter to the update action. Like the following in edit.html

<%= form_tag('/carts', method: 'put') do %>
  <%= number_field_tag "Quantity" %>
<% end %>

But then it complains there is no put route, when there is...

    cart GET    /carts/:id(.:format)         carts#show
         PATCH  /carts/:id(.:format)         carts#update
         PUT    /carts/:id(.:format)         carts#update

I would imagine this error is there because I don't have an :id so then I decided to override the put action like the following

  resources :carts, :except => 'put' do
    # put :carts
    put "/carts" => "carts#update"
  end

But I get the same no put route found error.

Any ideas?

2 Answers
Related