Apr 2, 2008
Generic-user-small theodore nor... 1 post

Topic: Agile Web Development with Rails / I'm getting an error and unable to figure it out...

I’m having the same problem

Any help would be greatly appreciated.

cart.rb -


class Cart
  attr_reader :items

  def initialize
    @items = []
  end

  def add_product(product)
    @items << product
  end
end

store_controller.rb


class StoreController < ApplicationController

  def index
    @products = Product.find_products_for_sale
  end

  def add_to_cart
    @cart = find_cart
    product = Product.find(params[:id])
    @card.add_product(product)
  end

  private

  def find_cart
    session[:cart] ||= Cart.new
  end

end

index.rhtml -


<h1>Your Pragmatic Catalog</h1>

<% for product in @products -%>
  <div class = "entry">
    <img src = "<%= product.image_url %>"/>
    <h3><%= h(product.title) %></h3>
    <%= product.description %>
    <span class = "price">
      <%= number_to_currency(product.price) %>
    </span>
    <%= button_to "Add to Cart",
                  :action => :add_to_cart,
                  :id => product %>
  </div>
<% end %>

1 post