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