biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-19 21:15 UTC
Remve/Update items in cart
I purchase che Agile Web Develoment with rails book so that i could
start learning RoR. I''ve worked through the depot applicationand it
works fine. I''ve even went as far ao to add more features. The Problem
is I can,t figure out how to edit/remove items frm the cart.
Here is my shopping cart view:
<% @page_title = "h3avystore cart" -%>
<% unless params[:context] == :checkout -%>
<div id=cartmenu>
<ul>
<li><%= link_to ''Continue shopping'', :action
=> "index" %></li>
<li><%= link_to ''Empty cart'', :action =>
"empty_cart" %></li>
<li><%= link_to ''Checkout'', :action =>
"checkout" %></li>
</ul>
</div>
<% end -%>
<table cellpadding=10 cellspacing=0 width=75%>
<tr class=carttitle>
<td rowspan=2>Description</td>
<td rowspan=2>Qty<product
<td colspan=2></td>
</tr>
<tr class=carttitle>
<td align=right>Price Each</td>
<td align=right>Total</td>
</tr>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= h(product.title) %> <%= link_to("(remove)",
:action
=> :remove_item, :id => product.id) %></td>
<td align=center><%= item.quantity %></td>
<td align=right><%= fmt_dollars(item.unit_price) %></td>
<td align=right><%= fmt_dollars(item.unit_price * item.quantity)
%></td>
</tr>
<% end %>
<tr>
<td colspan=3
align=right><strong>Total:</strong></td>
<td id=totalcell><%= fmt_dollars(@cart.total_price)
%></td>
</tr>
</table>
Here is my store contraller:
class StoreController < ApplicationController
before_filter :find_cart, :except => :index
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'')
rescue
logger.error("Attempt to access invalid product #{params[:id]}")
redirect_to_index(''Invalid product'')
end
def display_cart
@items = @cart.items
if @items.empty?
redirect_to_index("Your cart is currently empty")
end
if params[:context] == :checkout
render(:layout => false)
end
end
def remove_item
id = params[:id]
flash[:notice] = "Item has been removed from cart"
redirect_to(:action => :display_cart)
end
def empty_cart
@cart = find_cart
@cart.empty!
redirect_to_index(''Your cart is now empty'')
end
def checkout
@items = find_cart.items
if @items.empty?
redirect_to_index(''There is nothing in your cart!'')
else
@order = Order.new
end
end
def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.line_items << @cart.items
if @order.save
@cart.empty!
redirect_to_index(''Thank you for your order'')
else
render(:action => ''checkout'')
end
end
private
def find_cart
@cart = (session[:cart] ||= Cart.new)
end
end
Thats as much as i can get working. The id is getting passed and in
the correct produt but I cant figure out how to access that prduct to
change the quantity. Any help would be greatly appreciated. TIA
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
I am working on the same code at the moment. You should set up a few methods in your CartItem class -- check the book about that. Also, some of your code below is not exactly as it is the book -- but I don''t know enough to tell you where your problem is. Good luck, Elle On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I purchase che Agile Web Develoment with rails book so that i could > start learning RoR. I''ve worked through the depot applicationand it > works fine. I''ve even went as far ao to add more features. The Problem > is I can,t figure out how to edit/remove items frm the cart. > > Here is my shopping cart view: > > <% @page_title = "h3avystore cart" -%> > <% unless params[:context] == :checkout -%> > <div id=cartmenu> > <ul> > <li><%= link_to ''Continue shopping'', :action => "index" %></li> > <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > <li><%= link_to ''Checkout'', :action => "checkout" %></li> > </ul> > </div> > <% end -%> > > <table cellpadding=10 cellspacing=0 width=75%> > <tr class=carttitle> > <td rowspan=2>Description</td> > <td rowspan=2>Qty<product > <td colspan=2></td> > </tr> > <tr class=carttitle> > <td align=right>Price Each</td> > <td align=right>Total</td> > </tr> > <% > for item in @items > product = item.product > -%> > <tr> > <td><%= h(product.title) %> <%= link_to("(remove)", :action > => :remove_item, :id => product.id) %></td> > <td align=center><%= item.quantity %></td> > <td align=right><%= fmt_dollars(item.unit_price) %></td> > <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > %></td> > </tr> > <% end %> > <tr> > <td colspan=3 align=right><strong>Total:</strong></td> > <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > </tr> > </table> > > Here is my store contraller: > > class StoreController < ApplicationController > > before_filter :find_cart, :except => :index > > 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'') > rescue > logger.error("Attempt to access invalid product #{params[:id]}") > redirect_to_index(''Invalid product'') > end > > def display_cart > @items = @cart.items > if @items.empty? > redirect_to_index("Your cart is currently empty") > end > if params[:context] == :checkout > render(:layout => false) > end > end > > def remove_item > id = params[:id] > flash[:notice] = "Item has been removed from cart" > redirect_to(:action => :display_cart) > end > > def empty_cart > @cart = find_cart > @cart.empty! > redirect_to_index(''Your cart is now empty'') > end > > def checkout > @items = find_cart.items > if @items.empty? > redirect_to_index(''There is nothing in your cart!'') > else > @order = Order.new > end > end > > def save_order > @cart = find_cart > @order = Order.new(params[:order]) > @order.line_items << @cart.items > if @order.save > @cart.empty! > redirect_to_index(''Thank you for your order'') > else > render(:action => ''checkout'') > end > end > > private > > def find_cart > @cart = (session[:cart] ||= Cart.new) > end > end > > Thats as much as i can get working. The id is getting passed and in > the correct produt but I cant figure out how to access that prduct to > change the quantity. Any help would be greatly appreciated. TIA--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I am not actually working on the depot app, and never have, but I took a
quick look at it. Try adding this to your cart class:
def remove_from_cart(item)
@items.delete(item)
end
Then in your controller:
def remove_item
@cart = find_cart
product = Product.find params[:id]
if product
@cart.remove_from_cart(product)
flash[:notice] = "Item has been removed from cart"
else
flash[:error] = "Unable to find product"
end
redirect_to(:action => :display_cart)
end
Since I don''t have a depot app to test with, I can''t guarantee
this all
works perfectly, but it should point you in the right direction.
-Bill
elle wrote:> I am working on the same code at the moment.
> You should set up a few methods in your CartItem class -- check the
> book about that.
> Also, some of your code below is not exactly as it is the book -- but
> I don''t know enough to tell you where your problem is.
>
> Good luck,
> Elle
>
> On Oct 20, 9:15 am,
"biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>> I purchase che Agile Web Develoment with rails book so that i could
>> start learning RoR. I''ve worked through the depot
applicationand it
>> works fine. I''ve even went as far ao to add more features. The
Problem
>> is I can,t figure out how to edit/remove items frm the cart.
>>
>> Here is my shopping cart view:
>>
>> <% @page_title = "h3avystore cart" -%>
>> <% unless params[:context] == :checkout -%>
>> <div id=cartmenu>
>> <ul>
>> <li><%= link_to ''Continue shopping'',
:action => "index" %></li>
>> <li><%= link_to ''Empty cart'', :action
=> "empty_cart" %></li>
>> <li><%= link_to ''Checkout'', :action
=> "checkout" %></li>
>> </ul>
>> </div>
>> <% end -%>
>>
>> <table cellpadding=10 cellspacing=0 width=75%>
>> <tr class=carttitle>
>> <td rowspan=2>Description</td>
>> <td rowspan=2>Qty<product
>> <td colspan=2></td>
>> </tr>
>> <tr class=carttitle>
>> <td align=right>Price Each</td>
>> <td align=right>Total</td>
>> </tr>
>> <%
>> for item in @items
>> product = item.product
>> -%>
>> <tr>
>> <td><%= h(product.title) %> <%=
link_to("(remove)", :action
>> => :remove_item, :id => product.id) %></td>
>> <td align=center><%= item.quantity %></td>
>> <td align=right><%= fmt_dollars(item.unit_price)
%></td>
>> <td align=right><%= fmt_dollars(item.unit_price *
item.quantity)
>> %></td>
>> </tr>
>> <% end %>
>> <tr>
>> <td colspan=3
align=right><strong>Total:</strong></td>
>> <td id=totalcell><%= fmt_dollars(@cart.total_price)
%></td>
>> </tr>
>> </table>
>>
>> Here is my store contraller:
>>
>> class StoreController < ApplicationController
>>
>> before_filter :find_cart, :except => :index
>>
>> 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'')
>> rescue
>> logger.error("Attempt to access invalid product
#{params[:id]}")
>> redirect_to_index(''Invalid product'')
>> end
>>
>> def display_cart
>> @items = @cart.items
>> if @items.empty?
>> redirect_to_index("Your cart is currently empty")
>> end
>> if params[:context] == :checkout
>> render(:layout => false)
>> end
>> end
>>
>> def remove_item
>> id = params[:id]
>> flash[:notice] = "Item has been removed from cart"
>> redirect_to(:action => :display_cart)
>> end
>>
>> def empty_cart
>> @cart = find_cart
>> @cart.empty!
>> redirect_to_index(''Your cart is now empty'')
>> end
>>
>> def checkout
>> @items = find_cart.items
>> if @items.empty?
>> redirect_to_index(''There is nothing in your
cart!'')
>> else
>> @order = Order.new
>> end
>> end
>>
>> def save_order
>> @cart = find_cart
>> @order = Order.new(params[:order])
>> @order.line_items << @cart.items
>> if @order.save
>> @cart.empty!
>> redirect_to_index(''Thank you for your order'')
>> else
>> render(:action => ''checkout'')
>> end
>> end
>>
>> private
>>
>> def find_cart
>> @cart = (session[:cart] ||= Cart.new)
>> end
>> end
>>
>> Thats as much as i can get working. The id is getting passed and in
>> the correct produt but I cant figure out how to access that prduct to
>> change the quantity. Any help would be greatly appreciated. TIA
>>
>
>
> >
>
--
Sincerely,
William Pratt
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-20 20:49 UTC
Re: Remve/Update items in cart
That method produces no errors but also does nothing. It should also be montioned I have the first edition of the book and thats probably why my codo looks different. undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> I am not actually working on the depot app, and never have, but I took a > quick look at it. Try adding this to your cart class: > > def remove_from_cart(item) > @items.delete(item) > end > > Then in your controller: > > def remove_item > @cart = find_cart > product = Product.find params[:id] > if product > @cart.remove_from_cart(product) > flash[:notice] = "Item has been removed from cart" > else > flash[:error] = "Unable to find product" > end > redirect_to(:action => :display_cart) > end > > Since I don''t have a depot app to test with, I can''t guarantee this all > works perfectly, but it should point you in the right direction. > > -Bill > > > > elle wrote: > > I am working on the same code at the moment. > > You should set up a few methods in your CartItem class -- check the > > book about that. > > Also, some of your code below is not exactly as it is the book -- but > > I don''t know enough to tell you where your problem is. > > > Good luck, > > Elle > > > On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I purchase che Agile Web Develoment with rails book so that i could > >> start learning RoR. I''ve worked through the depot applicationand it > >> works fine. I''ve even went as far ao to add more features. The Problem > >> is I can,t figure out how to edit/remove items frm the cart. > > >> Here is my shopping cart view: > > >> <% @page_title = "h3avystore cart" -%> > >> <% unless params[:context] == :checkout -%> > >> <div id=cartmenu> > >> <ul> > >> <li><%= link_to ''Continue shopping'', :action => "index" %></li> > >> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > >> <li><%= link_to ''Checkout'', :action => "checkout" %></li> > >> </ul> > >> </div> > >> <% end -%> > > >> <table cellpadding=10 cellspacing=0 width=75%> > >> <tr class=carttitle> > >> <td rowspan=2>Description</td> > >> <td rowspan=2>Qty<product > >> <td colspan=2></td> > >> </tr> > >> <tr class=carttitle> > >> <td align=right>Price Each</td> > >> <td align=right>Total</td> > >> </tr> > >> <% > >> for item in @items > >> product = item.product > >> -%> > >> <tr> > >> <td><%= h(product.title) %> <%= link_to("(remove)", :action > >> => :remove_item, :id => product.id) %></td> > >> <td align=center><%= item.quantity %></td> > >> <td align=right><%= fmt_dollars(item.unit_price) %></td> > >> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > >> %></td> > >> </tr> > >> <% end %> > >> <tr> > >> <td colspan=3 align=right><strong>Total:</strong></td> > >> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > >> </tr> > >> </table> > > >> Here is my store contraller: > > >> class StoreController < ApplicationController > > >> before_filter :find_cart, :except => :index > > >> 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'') > >> rescue > >> logger.error("Attempt to access invalid product #{params[:id]}") > >> redirect_to_index(''Invalid product'') > >> end > > >> def display_cart > >> @items = @cart.items > >> if @items.empty? > >> redirect_to_index("Your cart is currently empty") > >> end > >> if params[:context] == :checkout > >> render(:layout => false) > >> end > >> end > > >> def remove_item > >> id = params[:id] > >> flash[:notice] = "Item has been removed from cart" > >> redirect_to(:action => :display_cart) > >> end > > >> def empty_cart > >> @cart = find_cart > >> @cart.empty! > >> redirect_to_index(''Your cart is now empty'') > >> end > > >> def checkout > >> @items = find_cart.items > >> if @items.empty? > >> redirect_to_index(''There is nothing in your cart!'') > >> else > >> @order = Order.new > >> end > >> end > > >> def save_order > >> @cart = find_cart > >> @order = Order.new(params[:order]) > >> @order.line_items << @cart.items > >> if @order.save > >> @cart.empty! > >> redirect_to_index(''Thank you for your order'') > >> else > >> render(:action => ''checkout'') > >> end > >> end > > >> private > > >> def find_cart > >> @cart = (session[:cart] ||= Cart.new) > >> end > >> end > > >> Thats as much as i can get working. The id is getting passed and in > >> the correct produt but I cant figure out how to access that prduct to > >> change the quantity. Any help would be greatly appreciated. TIA > > -- > Sincerely, > > William Pratt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-20 20:54 UTC
Re: Remve/Update items in cart
That solution didnt work. It did not produce any errors but didnt do anything either. Also I have the first editien of the book and that may be why my code looks different. On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> I am not actually working on the depot app, and never have, but I took a > quick look at it. Try adding this to your cart class: > > def remove_from_cart(item) > @items.delete(item) > end > > Then in your controller: > > def remove_item > @cart = find_cart > product = Product.find params[:id] > if product > @cart.remove_from_cart(product) > flash[:notice] = "Item has been removed from cart" > else > flash[:error] = "Unable to find product" > end > redirect_to(:action => :display_cart) > end > > Since I don''t have a depot app to test with, I can''t guarantee this all > works perfectly, but it should point you in the right direction. > > -Bill > > > > elle wrote: > > I am working on the same code at the moment. > > You should set up a few methods in your CartItem class -- check the > > book about that. > > Also, some of your code below is not exactly as it is the book -- but > > I don''t know enough to tell you where your problem is. > > > Good luck, > > Elle > > > On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I purchase che Agile Web Develoment with rails book so that i could > >> start learning RoR. I''ve worked through the depot applicationand it > >> works fine. I''ve even went as far ao to add more features. The Problem > >> is I can,t figure out how to edit/remove items frm the cart. > > >> Here is my shopping cart view: > > >> <% @page_title = "h3avystore cart" -%> > >> <% unless params[:context] == :checkout -%> > >> <div id=cartmenu> > >> <ul> > >> <li><%= link_to ''Continue shopping'', :action => "index" %></li> > >> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > >> <li><%= link_to ''Checkout'', :action => "checkout" %></li> > >> </ul> > >> </div> > >> <% end -%> > > >> <table cellpadding=10 cellspacing=0 width=75%> > >> <tr class=carttitle> > >> <td rowspan=2>Description</td> > >> <td rowspan=2>Qty<product > >> <td colspan=2></td> > >> </tr> > >> <tr class=carttitle> > >> <td align=right>Price Each</td> > >> <td align=right>Total</td> > >> </tr> > >> <% > >> for item in @items > >> product = item.product > >> -%> > >> <tr> > >> <td><%= h(product.title) %> <%= link_to("(remove)", :action > >> => :remove_item, :id => product.id) %></td> > >> <td align=center><%= item.quantity %></td> > >> <td align=right><%= fmt_dollars(item.unit_price) %></td> > >> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > >> %></td> > >> </tr> > >> <% end %> > >> <tr> > >> <td colspan=3 align=right><strong>Total:</strong></td> > >> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > >> </tr> > >> </table> > > >> Here is my store contraller: > > >> class StoreController < ApplicationController > > >> before_filter :find_cart, :except => :index > > >> 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'') > >> rescue > >> logger.error("Attempt to access invalid product #{params[:id]}") > >> redirect_to_index(''Invalid product'') > >> end > > >> def display_cart > >> @items = @cart.items > >> if @items.empty? > >> redirect_to_index("Your cart is currently empty") > >> end > >> if params[:context] == :checkout > >> render(:layout => false) > >> end > >> end > > >> def remove_item > >> id = params[:id] > >> flash[:notice] = "Item has been removed from cart" > >> redirect_to(:action => :display_cart) > >> end > > >> def empty_cart > >> @cart = find_cart > >> @cart.empty! > >> redirect_to_index(''Your cart is now empty'') > >> end > > >> def checkout > >> @items = find_cart.items > >> if @items.empty? > >> redirect_to_index(''There is nothing in your cart!'') > >> else > >> @order = Order.new > >> end > >> end > > >> def save_order > >> @cart = find_cart > >> @order = Order.new(params[:order]) > >> @order.line_items << @cart.items > >> if @order.save > >> @cart.empty! > >> redirect_to_index(''Thank you for your order'') > >> else > >> render(:action => ''checkout'') > >> end > >> end > > >> private > > >> def find_cart > >> @cart = (session[:cart] ||= Cart.new) > >> end > >> end > > >> Thats as much as i can get working. The id is getting passed and in > >> the correct produt but I cant figure out how to access that prduct to > >> change the quantity. Any help would be greatly appreciated. TIA > > -- > Sincerely, > > William Pratt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you are getting that error, then you did not put the remove_from_cart method I included in models/cart.rb or you misspelled it. If not, try restarting your server. biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> That method produces no errors but also does nothing. It should also > be montioned I have the first edition of the book and thats probably > why my codo looks different. > > undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> I am not actually working on the depot app, and never have, but I took a >> quick look at it. Try adding this to your cart class: >> >> def remove_from_cart(item) >> @items.delete(item) >> end >> >> Then in your controller: >> >> def remove_item >> @cart = find_cart >> product = Product.find params[:id] >> if product >> @cart.remove_from_cart(product) >> flash[:notice] = "Item has been removed from cart" >> else >> flash[:error] = "Unable to find product" >> end >> redirect_to(:action => :display_cart) >> end >> >> Since I don''t have a depot app to test with, I can''t guarantee this all >> works perfectly, but it should point you in the right direction. >> >> -Bill >> >> >> >> elle wrote: >> >>> I am working on the same code at the moment. >>> You should set up a few methods in your CartItem class -- check the >>> book about that. >>> Also, some of your code below is not exactly as it is the book -- but >>> I don''t know enough to tell you where your problem is. >>> >>> Good luck, >>> Elle >>> >>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>>> I purchase che Agile Web Develoment with rails book so that i could >>>> start learning RoR. I''ve worked through the depot applicationand it >>>> works fine. I''ve even went as far ao to add more features. The Problem >>>> is I can,t figure out how to edit/remove items frm the cart. >>>> >>>> Here is my shopping cart view: >>>> >>>> <% @page_title = "h3avystore cart" -%> >>>> <% unless params[:context] == :checkout -%> >>>> <div id=cartmenu> >>>> <ul> >>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> >>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> >>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> >>>> </ul> >>>> </div> >>>> <% end -%> >>>> >>>> <table cellpadding=10 cellspacing=0 width=75%> >>>> <tr class=carttitle> >>>> <td rowspan=2>Description</td> >>>> <td rowspan=2>Qty<product >>>> <td colspan=2></td> >>>> </tr> >>>> <tr class=carttitle> >>>> <td align=right>Price Each</td> >>>> <td align=right>Total</td> >>>> </tr> >>>> <% >>>> for item in @items >>>> product = item.product >>>> -%> >>>> <tr> >>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action >>>> => :remove_item, :id => product.id) %></td> >>>> <td align=center><%= item.quantity %></td> >>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> >>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) >>>> %></td> >>>> </tr> >>>> <% end %> >>>> <tr> >>>> <td colspan=3 align=right><strong>Total:</strong></td> >>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> >>>> </tr> >>>> </table> >>>> >>>> Here is my store contraller: >>>> >>>> class StoreController < ApplicationController >>>> >>>> before_filter :find_cart, :except => :index >>>> >>>> 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'') >>>> rescue >>>> logger.error("Attempt to access invalid product #{params[:id]}") >>>> redirect_to_index(''Invalid product'') >>>> end >>>> >>>> def display_cart >>>> @items = @cart.items >>>> if @items.empty? >>>> redirect_to_index("Your cart is currently empty") >>>> end >>>> if params[:context] == :checkout >>>> render(:layout => false) >>>> end >>>> end >>>> >>>> def remove_item >>>> id = params[:id] >>>> flash[:notice] = "Item has been removed from cart" >>>> redirect_to(:action => :display_cart) >>>> end >>>> >>>> def empty_cart >>>> @cart = find_cart >>>> @cart.empty! >>>> redirect_to_index(''Your cart is now empty'') >>>> end >>>> >>>> def checkout >>>> @items = find_cart.items >>>> if @items.empty? >>>> redirect_to_index(''There is nothing in your cart!'') >>>> else >>>> @order = Order.new >>>> end >>>> end >>>> >>>> def save_order >>>> @cart = find_cart >>>> @order = Order.new(params[:order]) >>>> @order.line_items << @cart.items >>>> if @order.save >>>> @cart.empty! >>>> redirect_to_index(''Thank you for your order'') >>>> else >>>> render(:action => ''checkout'') >>>> end >>>> end >>>> >>>> private >>>> >>>> def find_cart >>>> @cart = (session[:cart] ||= Cart.new) >>>> end >>>> end >>>> >>>> Thats as much as i can get working. The id is getting passed and in >>>> the correct produt but I cant figure out how to access that prduct to >>>> change the quantity. Any help would be greatly appreciated. TIA >>>> >> -- >> Sincerely, >> >> William Pratt >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-21 10:34 UTC
Re: Remve/Update items in cart
I''m not getting any errors, it just is''nt working. On Oct 20, 4:21 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> If you are getting that error, then you did not put the remove_from_cart > method I included in models/cart.rb or you misspelled it. If not, try > restarting your server. > > biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > That method produces no errors but also does nothing. It should also > > be montioned I have the first edition of the book and thats probably > > why my codo looks different. > > > undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > > On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> I am not actually working on the depot app, and never have, but I took a > >> quick look at it. Try adding this to your cart class: > > >> def remove_from_cart(item) > >> @items.delete(item) > >> end > > >> Then in your controller: > > >> def remove_item > >> @cart = find_cart > >> product = Product.find params[:id] > >> if product > >> @cart.remove_from_cart(product) > >> flash[:notice] = "Item has been removed from cart" > >> else > >> flash[:error] = "Unable to find product" > >> end > >> redirect_to(:action => :display_cart) > >> end > > >> Since I don''t have a depot app to test with, I can''t guarantee this all > >> works perfectly, but it should point you in the right direction. > > >> -Bill > > >> elle wrote: > > >>> I am working on the same code at the moment. > >>> You should set up a few methods in your CartItem class -- check the > >>> book about that. > >>> Also, some of your code below is not exactly as it is the book -- but > >>> I don''t know enough to tell you where your problem is. > > >>> Good luck, > >>> Elle > > >>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>> I purchase che Agile Web Develoment with rails book so that i could > >>>> start learning RoR. I''ve worked through the depot applicationand it > >>>> works fine. I''ve even went as far ao to add more features. The Problem > >>>> is I can,t figure out how to edit/remove items frm the cart. > > >>>> Here is my shopping cart view: > > >>>> <% @page_title = "h3avystore cart" -%> > >>>> <% unless params[:context] == :checkout -%> > >>>> <div id=cartmenu> > >>>> <ul> > >>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> > >>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > >>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> > >>>> </ul> > >>>> </div> > >>>> <% end -%> > > >>>> <table cellpadding=10 cellspacing=0 width=75%> > >>>> <tr class=carttitle> > >>>> <td rowspan=2>Description</td> > >>>> <td rowspan=2>Qty<product > >>>> <td colspan=2></td> > >>>> </tr> > >>>> <tr class=carttitle> > >>>> <td align=right>Price Each</td> > >>>> <td align=right>Total</td> > >>>> </tr> > >>>> <% > >>>> for item in @items > >>>> product = item.product > >>>> -%> > >>>> <tr> > >>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action > >>>> => :remove_item, :id => product.id) %></td> > >>>> <td align=center><%= item.quantity %></td> > >>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> > >>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > >>>> %></td> > >>>> </tr> > >>>> <% end %> > >>>> <tr> > >>>> <td colspan=3 align=right><strong>Total:</strong></td> > >>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > >>>> </tr> > >>>> </table> > > >>>> Here is my store contraller: > > >>>> class StoreController < ApplicationController > > >>>> before_filter :find_cart, :except => :index > > >>>> 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'') > >>>> rescue > >>>> logger.error("Attempt to access invalid product #{params[:id]}") > >>>> redirect_to_index(''Invalid product'') > >>>> end > > >>>> def display_cart > >>>> @items = @cart.items > >>>> if @items.empty? > >>>> redirect_to_index("Your cart is currently empty") > >>>> end > >>>> if params[:context] == :checkout > >>>> render(:layout => false) > >>>> end > >>>> end > > >>>> def remove_item > >>>> id = params[:id] > >>>> flash[:notice] = "Item has been removed from cart" > >>>> redirect_to(:action => :display_cart) > >>>> end > > >>>> def empty_cart > >>>> @cart = find_cart > >>>> @cart.empty! > >>>> redirect_to_index(''Your cart is now empty'') > >>>> end > > >>>> def checkout > >>>> @items = find_cart.items > >>>> if @items.empty? > >>>> redirect_to_index(''There is nothing in your cart!'') > >>>> else > >>>> @order = Order.new > >>>> end > >>>> end > > >>>> def save_order > >>>> @cart = find_cart > >>>> @order = Order.new(params[:order]) > >>>> @order.line_items << @cart.items > >>>> if @order.save > >>>> @cart.empty! > >>>> redirect_to_index(''Thank you for your order'') > >>>> else > >>>> render(:action => ''checkout'') > >>>> end > >>>> end > > >>>> private > > >>>> def find_cart > >>>> @cart = (session[:cart] ||= Cart.new) > >>>> end > >>>> end > > >>>> Thats as much as i can get working. The id is getting passed and in > >>>> the correct produt but I cant figure out how to access that prduct to > >>>> change the quantity. Any help would be greatly appreciated. TIA > > >> -- > >> Sincerely, > > >> William Pratt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Then what was this in your post?> > > undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> >Looks like an error to me? biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I''m not getting any errors, it just is''nt working. > > On Oct 20, 4:21 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> If you are getting that error, then you did not put the remove_from_cart >> method I included in models/cart.rb or you misspelled it. If not, try >> restarting your server. >> >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> >>> That method produces no errors but also does nothing. It should also >>> be montioned I have the first edition of the book and thats probably >>> why my codo looks different. >>> >>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> >>> >>> On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>> >>>> I am not actually working on the depot app, and never have, but I took a >>>> quick look at it. Try adding this to your cart class: >>>> >>>> def remove_from_cart(item) >>>> @items.delete(item) >>>> end >>>> >>>> Then in your controller: >>>> >>>> def remove_item >>>> @cart = find_cart >>>> product = Product.find params[:id] >>>> if product >>>> @cart.remove_from_cart(product) >>>> flash[:notice] = "Item has been removed from cart" >>>> else >>>> flash[:error] = "Unable to find product" >>>> end >>>> redirect_to(:action => :display_cart) >>>> end >>>> >>>> Since I don''t have a depot app to test with, I can''t guarantee this all >>>> works perfectly, but it should point you in the right direction. >>>> >>>> -Bill >>>> >>>> elle wrote: >>>> >>>>> I am working on the same code at the moment. >>>>> You should set up a few methods in your CartItem class -- check the >>>>> book about that. >>>>> Also, some of your code below is not exactly as it is the book -- but >>>>> I don''t know enough to tell you where your problem is. >>>>> >>>>> Good luck, >>>>> Elle >>>>> >>>>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>>> >>>>>> I purchase che Agile Web Develoment with rails book so that i could >>>>>> start learning RoR. I''ve worked through the depot applicationand it >>>>>> works fine. I''ve even went as far ao to add more features. The Problem >>>>>> is I can,t figure out how to edit/remove items frm the cart. >>>>>> >>>>>> Here is my shopping cart view: >>>>>> >>>>>> <% @page_title = "h3avystore cart" -%> >>>>>> <% unless params[:context] == :checkout -%> >>>>>> <div id=cartmenu> >>>>>> <ul> >>>>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> >>>>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> >>>>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> >>>>>> </ul> >>>>>> </div> >>>>>> <% end -%> >>>>>> >>>>>> <table cellpadding=10 cellspacing=0 width=75%> >>>>>> <tr class=carttitle> >>>>>> <td rowspan=2>Description</td> >>>>>> <td rowspan=2>Qty<product >>>>>> <td colspan=2></td> >>>>>> </tr> >>>>>> <tr class=carttitle> >>>>>> <td align=right>Price Each</td> >>>>>> <td align=right>Total</td> >>>>>> </tr> >>>>>> <% >>>>>> for item in @items >>>>>> product = item.product >>>>>> -%> >>>>>> <tr> >>>>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action >>>>>> => :remove_item, :id => product.id) %></td> >>>>>> <td align=center><%= item.quantity %></td> >>>>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> >>>>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) >>>>>> %></td> >>>>>> </tr> >>>>>> <% end %> >>>>>> <tr> >>>>>> <td colspan=3 align=right><strong>Total:</strong></td> >>>>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> >>>>>> </tr> >>>>>> </table> >>>>>> >>>>>> Here is my store contraller: >>>>>> >>>>>> class StoreController < ApplicationController >>>>>> >>>>>> before_filter :find_cart, :except => :index >>>>>> >>>>>> 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'') >>>>>> rescue >>>>>> logger.error("Attempt to access invalid product #{params[:id]}") >>>>>> redirect_to_index(''Invalid product'') >>>>>> end >>>>>> >>>>>> def display_cart >>>>>> @items = @cart.items >>>>>> if @items.empty? >>>>>> redirect_to_index("Your cart is currently empty") >>>>>> end >>>>>> if params[:context] == :checkout >>>>>> render(:layout => false) >>>>>> end >>>>>> end >>>>>> >>>>>> def remove_item >>>>>> id = params[:id] >>>>>> flash[:notice] = "Item has been removed from cart" >>>>>> redirect_to(:action => :display_cart) >>>>>> end >>>>>> >>>>>> def empty_cart >>>>>> @cart = find_cart >>>>>> @cart.empty! >>>>>> redirect_to_index(''Your cart is now empty'') >>>>>> end >>>>>> >>>>>> def checkout >>>>>> @items = find_cart.items >>>>>> if @items.empty? >>>>>> redirect_to_index(''There is nothing in your cart!'') >>>>>> else >>>>>> @order = Order.new >>>>>> end >>>>>> end >>>>>> >>>>>> def save_order >>>>>> @cart = find_cart >>>>>> @order = Order.new(params[:order]) >>>>>> @order.line_items << @cart.items >>>>>> if @order.save >>>>>> @cart.empty! >>>>>> redirect_to_index(''Thank you for your order'') >>>>>> else >>>>>> render(:action => ''checkout'') >>>>>> end >>>>>> end >>>>>> >>>>>> private >>>>>> >>>>>> def find_cart >>>>>> @cart = (session[:cart] ||= Cart.new) >>>>>> end >>>>>> end >>>>>> >>>>>> Thats as much as i can get working. The id is getting passed and in >>>>>> the correct produt but I cant figure out how to access that prduct to >>>>>> change the quantity. Any help would be greatly appreciated. TIA >>>>>> >>>> -- >>>> Sincerely, >>>> >>>> William Pratt >>>> > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-21 17:14 UTC
Re: Remve/Update items in cart
Yeah I accidently posted that. It was because I made a typo but i fixed it and the code doesnt do anything. On Oct 21, 11:47 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> Then what was this in your post? > > > > > undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > Looks like an error to me? > > > > biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I''m not getting any errors, it just is''nt working. > > > On Oct 20, 4:21 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> If you are getting that error, then you did not put the remove_from_cart > >> method I included in models/cart.rb or you misspelled it. If not, try > >> restarting your server. > > >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > >>> That method produces no errors but also does nothing. It should also > >>> be montioned I have the first edition of the book and thats probably > >>> why my codo looks different. > > >>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > >>> On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> I am not actually working on the depot app, and never have, but I took a > >>>> quick look at it. Try adding this to your cart class: > > >>>> def remove_from_cart(item) > >>>> @items.delete(item) > >>>> end > > >>>> Then in your controller: > > >>>> def remove_item > >>>> @cart = find_cart > >>>> product = Product.find params[:id] > >>>> if product > >>>> @cart.remove_from_cart(product) > >>>> flash[:notice] = "Item has been removed from cart" > >>>> else > >>>> flash[:error] = "Unable to find product" > >>>> end > >>>> redirect_to(:action => :display_cart) > >>>> end > > >>>> Since I don''t have a depot app to test with, I can''t guarantee this all > >>>> works perfectly, but it should point you in the right direction. > > >>>> -Bill > > >>>> elle wrote: > > >>>>> I am working on the same code at the moment. > >>>>> You should set up a few methods in your CartItem class -- check the > >>>>> book about that. > >>>>> Also, some of your code below is not exactly as it is the book -- but > >>>>> I don''t know enough to tell you where your problem is. > > >>>>> Good luck, > >>>>> Elle > > >>>>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>>>> I purchase che Agile Web Develoment with rails book so that i could > >>>>>> start learning RoR. I''ve worked through the depot applicationand it > >>>>>> works fine. I''ve even went as far ao to add more features. The Problem > >>>>>> is I can,t figure out how to edit/remove items frm the cart. > > >>>>>> Here is my shopping cart view: > > >>>>>> <% @page_title = "h3avystore cart" -%> > >>>>>> <% unless params[:context] == :checkout -%> > >>>>>> <div id=cartmenu> > >>>>>> <ul> > >>>>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> > >>>>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > >>>>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> > >>>>>> </ul> > >>>>>> </div> > >>>>>> <% end -%> > > >>>>>> <table cellpadding=10 cellspacing=0 width=75%> > >>>>>> <tr class=carttitle> > >>>>>> <td rowspan=2>Description</td> > >>>>>> <td rowspan=2>Qty<product > >>>>>> <td colspan=2></td> > >>>>>> </tr> > >>>>>> <tr class=carttitle> > >>>>>> <td align=right>Price Each</td> > >>>>>> <td align=right>Total</td> > >>>>>> </tr> > >>>>>> <% > >>>>>> for item in @items > >>>>>> product = item.product > >>>>>> -%> > >>>>>> <tr> > >>>>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action > >>>>>> => :remove_item, :id => product.id) %></td> > >>>>>> <td align=center><%= item.quantity %></td> > >>>>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> > >>>>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > >>>>>> %></td> > >>>>>> </tr> > >>>>>> <% end %> > >>>>>> <tr> > >>>>>> <td colspan=3 align=right><strong>Total:</strong></td> > >>>>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > >>>>>> </tr> > >>>>>> </table> > > >>>>>> Here is my store contraller: > > >>>>>> class StoreController < ApplicationController > > >>>>>> before_filter :find_cart, :except => :index > > >>>>>> 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'') > >>>>>> rescue > >>>>>> logger.error("Attempt to access invalid product #{params[:id]}") > >>>>>> redirect_to_index(''Invalid product'') > >>>>>> end > > >>>>>> def display_cart > >>>>>> @items = @cart.items > >>>>>> if @items.empty? > >>>>>> redirect_to_index("Your cart is currently empty") > >>>>>> end > >>>>>> if params[:context] == :checkout > >>>>>> render(:layout => false) > >>>>>> end > >>>>>> end > > >>>>>> def remove_item > >>>>>> id = params[:id] > >>>>>> flash[:notice] = "Item has been removed from cart" > >>>>>> redirect_to(:action => :display_cart) > >>>>>> end > > >>>>>> def empty_cart > >>>>>> @cart = find_cart > >>>>>> @cart.empty! > >>>>>> redirect_to_index(''Your cart is now empty'') > >>>>>> end > > >>>>>> def checkout > >>>>>> @items = find_cart.items > >>>>>> if @items.empty? > >>>>>> redirect_to_index(''There is nothing in your cart!'') > >>>>>> else > >>>>>> @order = Order.new > >>>>>> end > >>>>>> end > > >>>>>> def save_order > >>>>>> @cart = find_cart > >>>>>> @order = Order.new(params[:order]) > >>>>>> @order.line_items << @cart.items > >>>>>> if @order.save > >>>>>> @cart.empty! > >>>>>> redirect_to_index(''Thank you for your order'') > >>>>>> else > >>>>>> render(:action => ''checkout'') > >>>>>> end > >>>>>> end > > >>>>>> private > > >>>>>> def find_cart > >>>>>> @cart = (session[:cart] ||= Cart.new) > >>>>>> end > >>>>>> end > > >>>>>> Thats as much as i can get working. The id is getting passed and in > >>>>>> the correct produt but I cant figure out how to access that prduct to > >>>>>> change the quantity. Any help would be greatly appreciated. TIA > > >>>> -- > >>>> Sincerely, > > >>>> William Pratt > > -- > > Sincerely, > > William Pratt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You will need to paste your model and controller code for me to see what you are doing wrong. Paste it to pastie.caboo.se and send the link if you want me to look further. biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Yeah I accidently posted that. It was because I made a typo but i > fixed it and the code doesnt do anything. > > On Oct 21, 11:47 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> Then what was this in your post? >> >> >>>>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> >>>>> >> Looks like an error to me? >> >> >> >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> >>> I''m not getting any errors, it just is''nt working. >>> >>> On Oct 20, 4:21 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>> >>>> If you are getting that error, then you did not put the remove_from_cart >>>> method I included in models/cart.rb or you misspelled it. If not, try >>>> restarting your server. >>>> >>>> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >>>> >>>>> That method produces no errors but also does nothing. It should also >>>>> be montioned I have the first edition of the book and thats probably >>>>> why my codo looks different. >>>>> >>>>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> >>>>> >>>>> On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>>> >>>>>> I am not actually working on the depot app, and never have, but I took a >>>>>> quick look at it. Try adding this to your cart class: >>>>>> >>>>>> def remove_from_cart(item) >>>>>> @items.delete(item) >>>>>> end >>>>>> >>>>>> Then in your controller: >>>>>> >>>>>> def remove_item >>>>>> @cart = find_cart >>>>>> product = Product.find params[:id] >>>>>> if product >>>>>> @cart.remove_from_cart(product) >>>>>> flash[:notice] = "Item has been removed from cart" >>>>>> else >>>>>> flash[:error] = "Unable to find product" >>>>>> end >>>>>> redirect_to(:action => :display_cart) >>>>>> end >>>>>> >>>>>> Since I don''t have a depot app to test with, I can''t guarantee this all >>>>>> works perfectly, but it should point you in the right direction. >>>>>> >>>>>> -Bill >>>>>> >>>>>> elle wrote: >>>>>> >>>>>>> I am working on the same code at the moment. >>>>>>> You should set up a few methods in your CartItem class -- check the >>>>>>> book about that. >>>>>>> Also, some of your code below is not exactly as it is the book -- but >>>>>>> I don''t know enough to tell you where your problem is. >>>>>>> >>>>>>> Good luck, >>>>>>> Elle >>>>>>> >>>>>>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>>>>> >>>>>>>> I purchase che Agile Web Develoment with rails book so that i could >>>>>>>> start learning RoR. I''ve worked through the depot applicationand it >>>>>>>> works fine. I''ve even went as far ao to add more features. The Problem >>>>>>>> is I can,t figure out how to edit/remove items frm the cart. >>>>>>>> >>>>>>>> Here is my shopping cart view: >>>>>>>> >>>>>>>> <% @page_title = "h3avystore cart" -%> >>>>>>>> <% unless params[:context] == :checkout -%> >>>>>>>> <div id=cartmenu> >>>>>>>> <ul> >>>>>>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> >>>>>>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> >>>>>>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> >>>>>>>> </ul> >>>>>>>> </div> >>>>>>>> <% end -%> >>>>>>>> >>>>>>>> <table cellpadding=10 cellspacing=0 width=75%> >>>>>>>> <tr class=carttitle> >>>>>>>> <td rowspan=2>Description</td> >>>>>>>> <td rowspan=2>Qty<product >>>>>>>> <td colspan=2></td> >>>>>>>> </tr> >>>>>>>> <tr class=carttitle> >>>>>>>> <td align=right>Price Each</td> >>>>>>>> <td align=right>Total</td> >>>>>>>> </tr> >>>>>>>> <% >>>>>>>> for item in @items >>>>>>>> product = item.product >>>>>>>> -%> >>>>>>>> <tr> >>>>>>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action >>>>>>>> => :remove_item, :id => product.id) %></td> >>>>>>>> <td align=center><%= item.quantity %></td> >>>>>>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> >>>>>>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) >>>>>>>> %></td> >>>>>>>> </tr> >>>>>>>> <% end %> >>>>>>>> <tr> >>>>>>>> <td colspan=3 align=right><strong>Total:</strong></td> >>>>>>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> >>>>>>>> </tr> >>>>>>>> </table> >>>>>>>> >>>>>>>> Here is my store contraller: >>>>>>>> >>>>>>>> class StoreController < ApplicationController >>>>>>>> >>>>>>>> before_filter :find_cart, :except => :index >>>>>>>> >>>>>>>> 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'') >>>>>>>> rescue >>>>>>>> logger.error("Attempt to access invalid product #{params[:id]}") >>>>>>>> redirect_to_index(''Invalid product'') >>>>>>>> end >>>>>>>> >>>>>>>> def display_cart >>>>>>>> @items = @cart.items >>>>>>>> if @items.empty? >>>>>>>> redirect_to_index("Your cart is currently empty") >>>>>>>> end >>>>>>>> if params[:context] == :checkout >>>>>>>> render(:layout => false) >>>>>>>> end >>>>>>>> end >>>>>>>> >>>>>>>> def remove_item >>>>>>>> id = params[:id] >>>>>>>> flash[:notice] = "Item has been removed from cart" >>>>>>>> redirect_to(:action => :display_cart) >>>>>>>> end >>>>>>>> >>>>>>>> def empty_cart >>>>>>>> @cart = find_cart >>>>>>>> @cart.empty! >>>>>>>> redirect_to_index(''Your cart is now empty'') >>>>>>>> end >>>>>>>> >>>>>>>> def checkout >>>>>>>> @items = find_cart.items >>>>>>>> if @items.empty? >>>>>>>> redirect_to_index(''There is nothing in your cart!'') >>>>>>>> else >>>>>>>> @order = Order.new >>>>>>>> end >>>>>>>> end >>>>>>>> >>>>>>>> def save_order >>>>>>>> @cart = find_cart >>>>>>>> @order = Order.new(params[:order]) >>>>>>>> @order.line_items << @cart.items >>>>>>>> if @order.save >>>>>>>> @cart.empty! >>>>>>>> redirect_to_index(''Thank you for your order'') >>>>>>>> else >>>>>>>> render(:action => ''checkout'') >>>>>>>> end >>>>>>>> end >>>>>>>> >>>>>>>> private >>>>>>>> >>>>>>>> def find_cart >>>>>>>> @cart = (session[:cart] ||= Cart.new) >>>>>>>> end >>>>>>>> end >>>>>>>> >>>>>>>> Thats as much as i can get working. The id is getting passed and in >>>>>>>> the correct produt but I cant figure out how to access that prduct to >>>>>>>> change the quantity. Any help would be greatly appreciated. TIA >>>>>>>> >>>>>> -- >>>>>> Sincerely, >>>>>> >>>>>> William Pratt >>>>>> >> -- >> >> Sincerely, >> >> William Pratt >> > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-22 14:41 UTC
Re: Remve/Update items in cart
http://pastie.caboo.se/109708 is the link. It has all three relevant
files. Pleas let me know what you think. I''ve also discovered that I
can set the quantity to zero and the item will still be in the cart
with the new quantity. Below is the code responsible for adding items,
by changing the one to the desired quantity it will update but a
quantity of zero will not remove it.
def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
item = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end
On Oct 21, 12:35 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org>
wrote:> You will need to paste your model and controller code for me to see what
> you are doing wrong. Paste it to pastie.caboo.se and send the link if
> you want me to look further.
>
>
>
> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
> > Yeah I accidently posted that. It was because I made a typo but i
> > fixed it and the code doesnt do anything.
>
> > On Oct 21, 11:47 am, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> >> Then what was this in your post?
>
> >>>>> undefined method `remove_from_cart'' for
#<Cart:0x2aaaab864040>
>
> >> Looks like an error to me?
>
> >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
>
> >>> I''m not getting any errors, it just is''nt
working.
>
> >>> On Oct 20, 4:21 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> >>>> If you are getting that error, then you did not put the
remove_from_cart
> >>>> method I included in models/cart.rb or you misspelled it.
If not, try
> >>>> restarting your server.
>
> >>>> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
>
> >>>>> That method produces no errors but also does nothing.
It should also
> >>>>> be montioned I have the first edition of the book and
thats probably
> >>>>> why my codo looks different.
>
> >>>>> undefined method `remove_from_cart'' for
#<Cart:0x2aaaab864040>
>
> >>>>> On Oct 19, 7:33 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> >>>>>> I am not actually working on the depot app, and
never have, but I took a
> >>>>>> quick look at it. Try adding this to your cart
class:
>
> >>>>>> def remove_from_cart(item)
> >>>>>> @items.delete(item)
> >>>>>> end
>
> >>>>>> Then in your controller:
>
> >>>>>> def remove_item
> >>>>>> @cart = find_cart
> >>>>>> product = Product.find params[:id]
> >>>>>> if product
> >>>>>> @cart.remove_from_cart(product)
> >>>>>> flash[:notice] = "Item has been removed
from cart"
> >>>>>> else
> >>>>>> flash[:error] = "Unable to find
product"
> >>>>>> end
> >>>>>> redirect_to(:action => :display_cart)
> >>>>>> end
>
> >>>>>> Since I don''t have a depot app to test
with, I can''t guarantee this all
> >>>>>> works perfectly, but it should point you in the
right direction.
>
> >>>>>> -Bill
>
> >>>>>> elle wrote:
>
> >>>>>>> I am working on the same code at the moment.
> >>>>>>> You should set up a few methods in your
CartItem class -- check the
> >>>>>>> book about that.
> >>>>>>> Also, some of your code below is not exactly
as it is the book -- but
> >>>>>>> I don''t know enough to tell you where
your problem is.
>
> >>>>>>> Good luck,
> >>>>>>> Elle
>
> >>>>>>> On Oct 20, 9:15 am,
"biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> >>>>>>>> I purchase che Agile Web Develoment with
rails book so that i could
> >>>>>>>> start learning RoR. I''ve worked
through the depot applicationand it
> >>>>>>>> works fine. I''ve even went as far
ao to add more features. The Problem
> >>>>>>>> is I can,t figure out how to edit/remove
items frm the cart.
>
> >>>>>>>> Here is my shopping cart view:
>
> >>>>>>>> <% @page_title = "h3avystore
cart" -%>
> >>>>>>>> <% unless params[:context] == :checkout
-%>
> >>>>>>>> <div id=cartmenu>
> >>>>>>>> <ul>
> >>>>>>>> <li><%= link_to
''Continue shopping'', :action => "index"
%></li>
> >>>>>>>> <li><%= link_to
''Empty cart'', :action => "empty_cart"
%></li>
> >>>>>>>> <li><%= link_to
''Checkout'', :action => "checkout"
%></li>
> >>>>>>>> </ul>
> >>>>>>>> </div>
> >>>>>>>> <% end -%>
>
> >>>>>>>> <table cellpadding=10 cellspacing=0
width=75%>
> >>>>>>>> <tr class=carttitle>
> >>>>>>>> <td
rowspan=2>Description</td>
> >>>>>>>> <td rowspan=2>Qty<product
> >>>>>>>> <td colspan=2></td>
> >>>>>>>> </tr>
> >>>>>>>> <tr class=carttitle>
> >>>>>>>> <td align=right>Price
Each</td>
> >>>>>>>> <td align=right>Total</td>
> >>>>>>>> </tr>
> >>>>>>>> <%
> >>>>>>>> for item in @items
> >>>>>>>> product = item.product
> >>>>>>>> -%>
> >>>>>>>> <tr>
> >>>>>>>> <td><%= h(product.title)
%> <%= link_to("(remove)", :action
> >>>>>>>> => :remove_item, :id => product.id)
%></td>
> >>>>>>>> <td align=center><%=
item.quantity %></td>
> >>>>>>>> <td align=right><%=
fmt_dollars(item.unit_price) %></td>
> >>>>>>>> <td align=right><%=
fmt_dollars(item.unit_price * item.quantity)
> >>>>>>>> %></td>
> >>>>>>>> </tr>
> >>>>>>>> <% end %>
> >>>>>>>> <tr>
> >>>>>>>> <td colspan=3
align=right><strong>Total:</strong></td>
> >>>>>>>> <td id=totalcell><%=
fmt_dollars(@cart.total_price) %></td>
> >>>>>>>> </tr>
> >>>>>>>> </table>
>
> >>>>>>>> Here is my store contraller:
>
> >>>>>>>> class StoreController <
ApplicationController
>
> >>>>>>>> before_filter :find_cart, :except =>
:index
>
> >>>>>>>> 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'')
> >>>>>>>> rescue
> >>>>>>>> logger.error("Attempt to access
invalid product #{params[:id]}")
> >>>>>>>> redirect_to_index(''Invalid
product'')
> >>>>>>>> end
>
> >>>>>>>> def display_cart
> >>>>>>>> @items = @cart.items
> >>>>>>>> if @items.empty?
> >>>>>>>> redirect_to_index("Your cart is
currently empty")
> >>>>>>>> end
> >>>>>>>> if params[:context] == :checkout
> >>>>>>>> render(:layout => false)
> >>>>>>>> end
> >>>>>>>> end
>
> >>>>>>>> def remove_item
> >>>>>>>> id = params[:id]
> >>>>>>>> flash[:notice] = "Item has been
removed from cart"
> >>>>>>>> redirect_to(:action =>
:display_cart)
> >>>>>>>> end
>
> >>>>>>>> def empty_cart
> >>>>>>>> @cart = find_cart
> >>>>>>>> @cart.empty!
> >>>>>>>> redirect_to_index(''Your cart
is now empty'')
> >>>>>>>> end
>
> >>>>>>>> def checkout
> >>>>>>>> @items = find_cart.items
> >>>>>>>> if @items.empty?
> >>>>>>>> redirect_to_index(''There is
nothing in your cart!'')
> >>>>>>>> else
> >>>>>>>> @order = Order.new
> >>>>>>>> end
> >>>>>>>> end
>
> >>>>>>>> def save_order
> >>>>>>>> @cart = find_cart
> >>>>>>>> @order = Order.new(params[:order])
> >>>>>>>> @order.line_items << @cart.items
> >>>>>>>> if @order.save
> >>>>>>>> @cart.empty!
> >>>>>>>> redirect_to_index(''Thank
you for your order'')
> >>>>>>>> else
> >>>>>>>> render(:action =>
''checkout'')
> >>>>>>>> end
> >>>>>>>> end
>
> >>>>>>>> private
>
> >>>>>>>> def find_cart
> >>>>>>>> @cart = (session[:cart] ||= Cart.new)
> >>>>>>>> end
> >>>>>>>> end
>
> >>>>>>>> Thats as much as i can get working. The id
is getting passed and in
> >>>>>>>> the correct produt but I cant figure out
how to access that prduct to
> >>>>>>>> change the quantity. Any help would be
greatly appreciated. TIA
>
> >>>>>> --
> >>>>>> Sincerely,
>
> >>>>>> William Pratt
>
> >> --
>
> >> Sincerely,
>
> >> William Pratt
>
> --
>
> Sincerely,
>
> William Pratt
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-22 14:45 UTC
Re: Remve/Update items in cart
http://pastie.caboo.se/109708 is the link. On Oct 21, 12:35 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> You will need to paste your model and controller code for me to see what > you are doing wrong. Paste it to pastie.caboo.se and send the link if > you want me to look further. > > > > biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Yeah I accidently posted that. It was because I made a typo but i > > fixed it and the code doesnt do anything. > > > On Oct 21, 11:47 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> Then what was this in your post? > > >>>>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > >> Looks like an error to me? > > >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > >>> I''m not getting any errors, it just is''nt working. > > >>> On Oct 20, 4:21 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> If you are getting that error, then you did not put the remove_from_cart > >>>> method I included in models/cart.rb or you misspelled it. If not, try > >>>> restarting your server. > > >>>> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > >>>>> That method produces no errors but also does nothing. It should also > >>>>> be montioned I have the first edition of the book and thats probably > >>>>> why my codo looks different. > > >>>>> undefined method `remove_from_cart'' for #<Cart:0x2aaaab864040> > > >>>>> On Oct 19, 7:33 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>>>> I am not actually working on the depot app, and never have, but I took a > >>>>>> quick look at it. Try adding this to your cart class: > > >>>>>> def remove_from_cart(item) > >>>>>> @items.delete(item) > >>>>>> end > > >>>>>> Then in your controller: > > >>>>>> def remove_item > >>>>>> @cart = find_cart > >>>>>> product = Product.find params[:id] > >>>>>> if product > >>>>>> @cart.remove_from_cart(product) > >>>>>> flash[:notice] = "Item has been removed from cart" > >>>>>> else > >>>>>> flash[:error] = "Unable to find product" > >>>>>> end > >>>>>> redirect_to(:action => :display_cart) > >>>>>> end > > >>>>>> Since I don''t have a depot app to test with, I can''t guarantee this all > >>>>>> works perfectly, but it should point you in the right direction. > > >>>>>> -Bill > > >>>>>> elle wrote: > > >>>>>>> I am working on the same code at the moment. > >>>>>>> You should set up a few methods in your CartItem class -- check the > >>>>>>> book about that. > >>>>>>> Also, some of your code below is not exactly as it is the book -- but > >>>>>>> I don''t know enough to tell you where your problem is. > > >>>>>>> Good luck, > >>>>>>> Elle > > >>>>>>> On Oct 20, 9:15 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>>>>>> I purchase che Agile Web Develoment with rails book so that i could > >>>>>>>> start learning RoR. I''ve worked through the depot applicationand it > >>>>>>>> works fine. I''ve even went as far ao to add more features. The Problem > >>>>>>>> is I can,t figure out how to edit/remove items frm the cart. > > >>>>>>>> Here is my shopping cart view: > > >>>>>>>> <% @page_title = "h3avystore cart" -%> > >>>>>>>> <% unless params[:context] == :checkout -%> > >>>>>>>> <div id=cartmenu> > >>>>>>>> <ul> > >>>>>>>> <li><%= link_to ''Continue shopping'', :action => "index" %></li> > >>>>>>>> <li><%= link_to ''Empty cart'', :action => "empty_cart" %></li> > >>>>>>>> <li><%= link_to ''Checkout'', :action => "checkout" %></li> > >>>>>>>> </ul> > >>>>>>>> </div> > >>>>>>>> <% end -%> > > >>>>>>>> <table cellpadding=10 cellspacing=0 width=75%> > >>>>>>>> <tr class=carttitle> > >>>>>>>> <td rowspan=2>Description</td> > >>>>>>>> <td rowspan=2>Qty<product > >>>>>>>> <td colspan=2></td> > >>>>>>>> </tr> > >>>>>>>> <tr class=carttitle> > >>>>>>>> <td align=right>Price Each</td> > >>>>>>>> <td align=right>Total</td> > >>>>>>>> </tr> > >>>>>>>> <% > >>>>>>>> for item in @items > >>>>>>>> product = item.product > >>>>>>>> -%> > >>>>>>>> <tr> > >>>>>>>> <td><%= h(product.title) %> <%= link_to("(remove)", :action > >>>>>>>> => :remove_item, :id => product.id) %></td> > >>>>>>>> <td align=center><%= item.quantity %></td> > >>>>>>>> <td align=right><%= fmt_dollars(item.unit_price) %></td> > >>>>>>>> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) > >>>>>>>> %></td> > >>>>>>>> </tr> > >>>>>>>> <% end %> > >>>>>>>> <tr> > >>>>>>>> <td colspan=3 align=right><strong>Total:</strong></td> > >>>>>>>> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> > >>>>>>>> </tr> > >>>>>>>> </table> > > >>>>>>>> Here is my store contraller: > > >>>>>>>> class StoreController < ApplicationController > > >>>>>>>> before_filter :find_cart, :except => :index > > >>>>>>>> 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'') > >>>>>>>> rescue > >>>>>>>> logger.error("Attempt to access invalid product #{params[:id]}") > >>>>>>>> redirect_to_index(''Invalid product'') > >>>>>>>> end > > >>>>>>>> def display_cart > >>>>>>>> @items = @cart.items > >>>>>>>> if @items.empty? > >>>>>>>> redirect_to_index("Your cart is currently empty") > >>>>>>>> end > >>>>>>>> if params[:context] == :checkout > >>>>>>>> render(:layout => false) > >>>>>>>> end > >>>>>>>> end > > >>>>>>>> def remove_item > >>>>>>>> id = params[:id] > >>>>>>>> flash[:notice] = "Item has been removed from cart" > >>>>>>>> redirect_to(:action => :display_cart) > >>>>>>>> end > > >>>>>>>> def empty_cart > >>>>>>>> @cart = find_cart > >>>>>>>> @cart.empty! > >>>>>>>> redirect_to_index(''Your cart is now empty'') > >>>>>>>> end > > >>>>>>>> def checkout > >>>>>>>> @items = find_cart.items > >>>>>>>> if @items.empty? > >>>>>>>> redirect_to_index(''There is nothing in your cart!'') > >>>>>>>> else > >>>>>>>> @order = Order.new > >>>>>>>> end > >>>>>>>> end > > >>>>>>>> def save_order > >>>>>>>> @cart = find_cart > >>>>>>>> @order = Order.new(params[:order]) > >>>>>>>> @order.line_items << @cart.items > >>>>>>>> if @order.save > >>>>>>>> @cart.empty! > >>>>>>>> redirect_to_index(''Thank you for your order'') > >>>>>>>> else > >>>>>>>> render(:action => ''checkout'') > >>>>>>>> end > >>>>>>>> end > > >>>>>>>> private > > >>>>>>>> def find_cart > >>>>>>>> @cart = (session[:cart] ||= Cart.new) > >>>>>>>> end > >>>>>>>> end > > >>>>>>>> Thats as much as i can get working. The id is getting passed and in > >>>>>>>> the correct produt but I cant figure out how to access that prduct to > >>>>>>>> change the quantity. Any help would be greatly appreciated. TIA > > >>>>>> -- > >>>>>> Sincerely, > > >>>>>> William Pratt > > >> -- > > >> Sincerely, > > >> William Pratt > > -- > > Sincerely, > > William Pratt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
biqut2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-22 17:54 UTC
Re: Remve/Update items in cart
NM I aftir looxinc at the code again I figured it out on my own. Here
is the solution:
<%= link_to("<font color=red>(remove)</font>", :action
=> :remove_item, :id => product.id) %>
def remove_item
product = Product.find(params[:id])
@cart.del_product(product)
flash[:notice] = "Item has been removed"
redirect_to(:action => :display_cart)
end
def del_product(product)
item = @items.find {|i| i.product_id == product.id}
temp_price = product.price * item.quantity
@items.delete(item)
@total_price -= temp_price
end
On Oct 22, 9:41 am, "biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> http://pastie.caboo.se/109708is the link. It has all three relevant
> files. Pleas let me know what you think. I''ve also discovered that
I
> can set the quantity to zero and the item will still be in the cart
> with the new quantity. Below is the code responsible for adding items,
> by changing the one to the desired quantity it will update but a
> quantity of zero will not remove it.
>
> def add_product(product)
> item = @items.find {|i| i.product_id == product.id}
> if item
> item.quantity += 1
> else
> item = LineItem.for_product(product)
> @items << item
> end
> @total_price += product.price
> end
>
> On Oct 21, 12:35 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> > You will need to paste your model and controller code for me to see
what
> > you are doing wrong. Paste it to pastie.caboo.se and send the link if
> > you want me to look further.
>
> > biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
> > > Yeah I accidently posted that. It was because I made a typo but i
> > > fixed it and the code doesnt do anything.
>
> > > On Oct 21, 11:47 am, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> > >> Then what was this in your post?
>
> > >>>>> undefined method `remove_from_cart'' for
#<Cart:0x2aaaab864040>
>
> > >> Looks like an error to me?
>
> > >> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
>
> > >>> I''m not getting any errors, it just
is''nt working.
>
> > >>> On Oct 20, 4:21 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> > >>>> If you are getting that error, then you did not put
the remove_from_cart
> > >>>> method I included in models/cart.rb or you misspelled
it. If not, try
> > >>>> restarting your server.
>
> > >>>> biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
>
> > >>>>> That method produces no errors but also does
nothing. It should also
> > >>>>> be montioned I have the first edition of the book
and thats probably
> > >>>>> why my codo looks different.
>
> > >>>>> undefined method `remove_from_cart'' for
#<Cart:0x2aaaab864040>
>
> > >>>>> On Oct 19, 7:33 pm, William Pratt
<bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:
>
> > >>>>>> I am not actually working on the depot app,
and never have, but I took a
> > >>>>>> quick look at it. Try adding this to your
cart class:
>
> > >>>>>> def remove_from_cart(item)
> > >>>>>> @items.delete(item)
> > >>>>>> end
>
> > >>>>>> Then in your controller:
>
> > >>>>>> def remove_item
> > >>>>>> @cart = find_cart
> > >>>>>> product = Product.find params[:id]
> > >>>>>> if product
> > >>>>>> @cart.remove_from_cart(product)
> > >>>>>> flash[:notice] = "Item has been
removed from cart"
> > >>>>>> else
> > >>>>>> flash[:error] = "Unable to find
product"
> > >>>>>> end
> > >>>>>> redirect_to(:action => :display_cart)
> > >>>>>> end
>
> > >>>>>> Since I don''t have a depot app to
test with, I can''t guarantee this all
> > >>>>>> works perfectly, but it should point you in
the right direction.
>
> > >>>>>> -Bill
>
> > >>>>>> elle wrote:
>
> > >>>>>>> I am working on the same code at the
moment.
> > >>>>>>> You should set up a few methods in your
CartItem class -- check the
> > >>>>>>> book about that.
> > >>>>>>> Also, some of your code below is not
exactly as it is the book -- but
> > >>>>>>> I don''t know enough to tell you
where your problem is.
>
> > >>>>>>> Good luck,
> > >>>>>>> Elle
>
> > >>>>>>> On Oct 20, 9:15 am,
"biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<biq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > >>>>>>>> I purchase che Agile Web Develoment
with rails book so that i could
> > >>>>>>>> start learning RoR. I''ve
worked through the depot applicationand it
> > >>>>>>>> works fine. I''ve even went
as far ao to add more features. The Problem
> > >>>>>>>> is I can,t figure out how to
edit/remove items frm the cart.
>
> > >>>>>>>> Here is my shopping cart view:
>
> > >>>>>>>> <% @page_title = "h3avystore
cart" -%>
> > >>>>>>>> <% unless params[:context] ==
:checkout -%>
> > >>>>>>>> <div id=cartmenu>
> > >>>>>>>> <ul>
> > >>>>>>>> <li><%= link_to
''Continue shopping'', :action => "index"
%></li>
> > >>>>>>>> <li><%= link_to
''Empty cart'', :action => "empty_cart"
%></li>
> > >>>>>>>> <li><%= link_to
''Checkout'', :action => "checkout"
%></li>
> > >>>>>>>> </ul>
> > >>>>>>>> </div>
> > >>>>>>>> <% end -%>
>
> > >>>>>>>> <table cellpadding=10
cellspacing=0 width=75%>
> > >>>>>>>> <tr class=carttitle>
> > >>>>>>>> <td
rowspan=2>Description</td>
> > >>>>>>>> <td
rowspan=2>Qty<product
> > >>>>>>>> <td colspan=2></td>
> > >>>>>>>> </tr>
> > >>>>>>>> <tr class=carttitle>
> > >>>>>>>> <td align=right>Price
Each</td>
> > >>>>>>>> <td
align=right>Total</td>
> > >>>>>>>> </tr>
> > >>>>>>>> <%
> > >>>>>>>> for item in @items
> > >>>>>>>> product = item.product
> > >>>>>>>> -%>
> > >>>>>>>> <tr>
> > >>>>>>>> <td><%= h(product.title)
%> <%= link_to("(remove)", :action
> > >>>>>>>> => :remove_item, :id =>
product.id) %></td>
> > >>>>>>>> <td align=center><%=
item.quantity %></td>
> > >>>>>>>> <td align=right><%=
fmt_dollars(item.unit_price) %></td>
> > >>>>>>>> <td align=right><%=
fmt_dollars(item.unit_price * item.quantity)
> > >>>>>>>> %></td>
> > >>>>>>>> </tr>
> > >>>>>>>> <% end %>
> > >>>>>>>> <tr>
> > >>>>>>>> <td colspan=3
align=right><strong>Total:</strong></td>
> > >>>>>>>> <td id=totalcell><%=
fmt_dollars(@cart.total_price) %></td>
> > >>>>>>>> </tr>
> > >>>>>>>> </table>
>
> > >>>>>>>> Here is my store contraller:
>
> > >>>>>>>> class StoreController <
ApplicationController
>
> > >>>>>>>> before_filter :find_cart, :except
=> :index
>
> > >>>>>>>> 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'')
> > >>>>>>>> rescue
> > >>>>>>>> logger.error("Attempt to
access invalid product #{params[:id]}")
> > >>>>>>>>
redirect_to_index(''Invalid product'')
> > >>>>>>>> end
>
> > >>>>>>>> def display_cart
> > >>>>>>>> @items = @cart.items
> > >>>>>>>> if @items.empty?
> > >>>>>>>> redirect_to_index("Your
cart is currently empty")
> > >>>>>>>> end
> > >>>>>>>> if params[:context] == :checkout
> > >>>>>>>> render(:layout => false)
> > >>>>>>>> end
> > >>>>>>>> end
>
> > >>>>>>>> def remove_item
> > >>>>>>>> id = params[:id]
> > >>>>>>>> flash[:notice] = "Item has
been removed from cart"
> > >>>>>>>> redirect_to(:action =>
:display_cart)
> > >>>>>>>> end
>
> > >>>>>>>> def empty_cart
> > >>>>>>>> @cart = find_cart
> > >>>>>>>> @cart.empty!
> > >>>>>>>> redirect_to_index(''Your
cart is now empty'')
> > >>>>>>>> end
>
> > >>>>>>>> def checkout
> > >>>>>>>> @items = find_cart.items
> > >>>>>>>> if @items.empty?
> > >>>>>>>>
redirect_to_index(''There is nothing in your cart!'')
> > >>>>>>>> else
> > >>>>>>>> @order = Order.new
> > >>>>>>>> end
> > >>>>>>>> end
>
> > >>>>>>>> def save_order
> > >>>>>>>> @cart = find_cart
> > >>>>>>>> @order =
Order.new(params[:order])
> > >>>>>>>> @order.line_items <<
@cart.items
> > >>>>>>>> if @order.save
> > >>>>>>>> @cart.empty!
> > >>>>>>>>
redirect_to_index(''Thank you for your order'')
> > >>>>>>>> else
> > >>>>>>>> render(:action =>
''checkout'')
> > >>>>>>>> end
> > >>>>>>>> end
>
> > >>>>>>>> private
>
> > >>>>>>>> def find_cart
> > >>>>>>>> @cart = (session[:cart] ||=
Cart.new)
> > >>>>>>>> end
> > >>>>>>>> end
>
> > >>>>>>>> Thats as much as i can get working.
The id is getting passed and in
> > >>>>>>>> the correct produt but I cant figure
out how to access that prduct to
> > >>>>>>>> change the quantity. Any help would
be greatly appreciated. TIA
>
> > >>>>>> --
> > >>>>>> Sincerely,
>
> > >>>>>> William Pratt
>
> > >> --
>
> > >> Sincerely,
>
> > >> William Pratt
>
> > --
>
> > Sincerely,
>
> > William Pratt
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---