Skip to content

Instantly share code, notes, and snippets.

@safarista
Created October 5, 2011 19:01
Show Gist options
  • Save safarista/1265332 to your computer and use it in GitHub Desktop.
Save safarista/1265332 to your computer and use it in GitHub Desktop.
I am trying to set more than one id in the add action.
class CartController < ApplicationController
before_filter :login_required
before_filter :find_cart
def add
@cart.save if @cart.new_record?
session[:cart_id] = @cart.id
recipe = Recipe.find(params[:id])
LineItem.create! :order => @cart, :recipe => recipe, :price => recipe.price
@cart.recalculate_price!
flash[:notice] = "Recipe added to cart"
redirect_to '/cart'
end
def remove
item = @cart.line_items.find(params[:id])
item.destroy
@cart.recalculate_price!
flash[:notice] = 'Item removed from cart'
redirect_to '/cart'
end
def checkout
@cart.checkout!
end
private
def find_cart
@cart = session[:cart_id] ? Order.find(session[:cart_id]) : current_user.orders.build
end
end
<!-- Recipes to be submitted -->
<%= form_tag "/cart/add/:id", :method => :get do %>
<table cellpadding="0" cellspacing="0" border="0" class="display">
<thead>
<tr>
<th><input type="checkbox" class="headSelect"/></th>
<th>Recipe #</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<% @recipes.each do |recipe| %>
<tr >
<td><%= check_box_tag recipe.name, recipe.id %></td>
<td>RCP-<%= recipe.id %></td>
<td><%= link_to recipe.name, recipe_path(recipe) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Submit for processing", :disable_with => "Please wait..." %>
<% end %>
# Cart
get "cart" => "cart#show"
get "cart/add/:id" => "cart#add", :as => :add_to_cart
post "cart/remove/:id" => "cart#remove", :as => :remove_from_cart
post "cart/checkout" => "cart#checkout", :as => :checkout
@safarista
Copy link
Author

ActiveRecord::RecordNotFound in CartController#add

Couldn't find Recipe with id=:id

Rails.root: /Users/safarista/github/menumenu
Application Trace | Framework Trace | Full Trace

app/controllers/cart_controller.rb:8:in `add'

Request

Parameters:

{"utf8"=>"✓",
"apple-crumble"=>"3",
"commit"=>"Submit for processing",
"id"=>":id"} <<<<<=== there is a problem here

Show session dump

_csrf_token: "89jNnu2MDJLhKn8Sod7GySAPT6HcnzWb5+mkop8NyTs="
cart_id: 8
session_id: "e5d71b5545e4a2cf47a0b0a524fb6b2b"
user_id: 1

@safarista
Copy link
Author

I am thinking my :url and :controller => :cart, :action => :add are not working very well. Am probably not processing the array.

Have an extra eye, HEEEEEEEEEEELP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment