Problem in agile web development examples

Hi everybody,
I am trying to implement the examples in the book “agile web development
with rails”, I hope that someone will help overcome a issue I couldn’t
resolve.
I have written the code given in example in the chapter 8 “Task C: cart
creation”.

While I have done every steps, I have a problem displaying my cart.
it could’nt change, it still empty while I click on “add to cart”
action.
any help will be appreciated

best reagrds,

On 12/18/06, Mustapha M. [email protected] wrote:

I have written the code given in example in the chapter 8 “Task C: cart
creation”.

While I have done every steps, I have a problem displaying my cart.
it could’nt change, it still empty while I click on “add to cart”
action.
any help will be appreciated

What do your logs say - find out what params are you passing into the
action?

hi,
here is my developemnt.log:
Processing StoreController#add_to_cart (for 127.0.0.1 at 2006-12-18
17:36:56) [GET]
Session ID: a9dda3bc03a9926d93c52ad383a4a158
Parameters: {“action”=>“add_to_cart”, “id”=>“3”,
“controller”=>“store”}
Product Load (0.000917) SELECT * FROM products WHERE (products.id =
‘3’) LIMIT 1
LineItem Columns (0.002014) SHOW FIELDS FROM line_items
Product Columns (0.002009) SHOW FIELDS FROM products
Redirected to http://localhost:3000/store/display_cart
Completed in 0.01195 (83 reqs/sec) | DB: 0.00494 (41%) | 302 Found
[http://localhost/store/add_to_cart/3]

Processing StoreController#display_cart (for 127.0.0.1 at 2006-12-18
17:36:56) [GET]
Session ID: a9dda3bc03a9926d93c52ad383a4a158
Parameters: {“action”=>“display_cart”, “controller”=>“store”}
Rendering within layouts/store
Rendering store/display_cart
Completed in 0.00289 (345 reqs/sec) | Rendering: 0.00163 (56%) | DB:
0.00000 (0%) | 200 OK [http://localhost/store/display_cart]

I can figure out the error, the expecting result is the display a non
null value size of the cart.

best regards,

Hi, can you post the relevant code that’s generatting the error message
as
well as more of your log. It’s not clear to me as to where the error is
happening in what you posted.

-Conrad

hi conrad,
there is no error message, it’s an unexpected result, my application is
a simple store with ‘add to cart’. and while I am selecting a product,
the cart remains empty, and loking at the developement log file, the
connection to the coresponding database succeed. I cant figure out where
the error would be hiding.
here some codes (you will find it in the book mentionned before):

store_controller.rb***
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end
def display_cart
@cart = find_cart
@items = @cart.items
@status = @items.empty?
end
private
def find_cart
session[:cart] || Cart.new
end
end


models/cart.rb
class Cart

attr_reader :items
attr_reader :total_price

def initialize
@items = []
@total_price = 0.0
end
#l’erreur est localisée ici
def add_product(product)
@items << LineItem.for_product(product) # il reste vide
@total_price += product.price # il reste nul
end
end


models/line_items.rb
class LineItem < ActiveRecord::Base

belongs_to :product

def self.for_product(product)
item = self.new
item.quantity = 1
item.product = product
item.unit_price = product.price
item
end
end


models/order.rb
class Order < ActiveRecord::Base
has_many :line_items
end


models/product.rb
class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validates_format_of :image_url,
:with => %r{^http:.+.(gif|jpg|png)$},
:message => “must be a url for gif, jpg,or png image”
def self.salable_items
find(:all,
:conditions => “date_available <= now()”,
:order => “date_available desc”)
end
protected
def validate
errors.add(:price, “should be positive”) unless price.nil? || price

0.0
end

end


views/display_cart.rhtml

Display cart

empty = <%= @status %>
You cart contains <%= @items.length %> items

****************************************************************************** views/index.rhtml <% for product in @products %>

<%= h(product.title) %>

<%= product.description %> <%= sprintf("$%0.2f", product.price) %> <%= link_to 'Add to Cart', {:action => 'add_to_cart', :id => product}, :class => 'addtocart' %>
 
<% end %> <%= link_to 'show my cart', :action => 'display_cart' %> ******************************************************************************* views/layouts/store.rhtml My rails web application example <% stylesheet_link_tag "depot", :media => "all" %>
<% @page_title || "My rails example" %>
<%= @content_for_layout %>
********************************************************************************

that’s the code of the example I am studying from the book.
as I said before, there is no error message, it’s the result of
‘add_to_cart’ which is flase, it remains empty even I have selected many
products.

best regards,

Conrad T. wrote:

Hi, can you post the relevant code that’s generatting the error message
as
well as more of your log. It’s not clear to me as to where the error is
happening in what you posted.

-Conrad

Mustapha M. wrote:

store_controller.rb***
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end
def display_cart
@cart = find_cart
@items = @cart.items
@status = @items.empty?
end
private
def find_cart
session[:cart] || Cart.new
end
end


find_cart should probably be session[:cart] ||= Cart.new (note the =
sign)

you’re never storng the cart in session, just getting a new one each
time.

HTH

Alan

thank you very much, I can’t beleive that!!!
I have checked my code many times but I haven’t noticed this error

thanks a lot
it’s a newbie mistake

Alan F. wrote:

Mustapha M. wrote:

store_controller.rb***
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end
def display_cart
@cart = find_cart
@items = @cart.items
@status = @items.empty?
end
private
def find_cart
session[:cart] || Cart.new
end
end


find_cart should probably be session[:cart] ||= Cart.new (note the =
sign)

you’re never storng the cart in session, just getting a new one each
time.

HTH

Alan