# Problem in agile web development examples

**URL:** <https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743>\
**Category:** Rails\
**Created:** [December 18, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743 "2006-12-18T18:15:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 18, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/1 "2006-12-18T18:15:01Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 18, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/2 "2006-12-18T18:23:50Z")

</div>

On 12/18/06, Mustapha M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 18, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/3 "2006-12-18T18:41:26Z")

</div>

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](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](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](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,

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 18, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/4 "2006-12-18T21:32:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 19, 2006, 12:00pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/5 "2006-12-19T12:00:13Z")

</div>

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' %\>  

&nbsp;
\<% 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

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 19, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/6 "2006-12-19T18:32:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mustapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mustapha](https://www.ruby-forum.com/u/mustapha)\
**Post date:** [December 19, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/problem-in-agile-web-development-examples/79743/7 "2006-12-19T18:42:17Z")

</div>

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
