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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/18/06, Mustapha Mustapha <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 appreciatedWhat do your logs say - find out what params are you passing into the action? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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,
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On 12/18/06, Mustapha Mustapha <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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, > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
<h1> Display cart</h1>
<p>
empty = <%= @status %><br/>
You cart contains <%= @items.length %> items <br/>
</p>
******************************************************************************
views/index.rhtml
<% for product in @products %>
<div class="catalogentry">
<img src="<%= product.image_url %>" />
<h3><%= h(product.title) %></h3>
<%= product.description %>
<span class="catalogprice"><%= sprintf("$%0.2f",
product.price)
%></span>
<%= link_to ''Add to Cart'',
{:action => ''add_to_cart'', :id => product},
:class => ''addtocart'' %><br/>
</div>
<div class="separator"> </div>
<% end %>
<%= link_to ''show my cart'', :action =>
''display_cart'' %>
*******************************************************************************
views/layouts/store.rhtml
<html>
<head>
<title> My rails web application example</title>
<% stylesheet_link_tag "depot", :media => "all" %>
</head>
<body>
<div id="banner">
<% @page_title || "My rails example" %>
</div>
<div id="columns">
<div id="side">
</div>
<div id="main">
<%= @content_for_layout %>
</div>
</div>
</body>
</html>
********************************************************************************
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 Taylor 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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mustapha Mustapha 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Francis wrote:> Mustapha Mustapha 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---