hi all..
I''ve been studying ROR using the agile development ROR book.
and I''m stuck at the shopping cart.
what I am trying to do is that, from the book, the
''add_to_cart''
button doesn''t take the quantity from the users.
well, What I am trying to do is that allowing users to input quantity
value
before pressing ''add_to_cart'' and when
''add_to_cart'' is pressed, the
users can see
the input values with the total price before checking out the items.
So, on the view, I added the following line.
Qty : <%= text_field_tag ''quantity'', ''1'',
:size => 2, :maxlength => 2
%>
<% form_remote_tag :url => {:action => ''add_to_cart'',
:id => product }
do %>
<%= submit_tag "Add to Cart" %>
<% end %>
and the store_controller,
quantity = Integer(params[''quantity''])
well..
the flashing cart shows 0 quantity.
i need help, please?
///////////////
Your Cart
0(quantity) × 1(prod_id) $0.00
Total $0.00
////////////////
--------------------------- (controller)
def add_to_cart
begin
product = Product.find(params[:id])
quantity = Integer(params[''quantity'']) #------ i hope this
is
right way to get quantity from the view
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid product #{params[:id]}" )
redirect_to_index("Invalid product" )
else
@cart = find_cart
@current_item = @cart.add_product(product, quantity)
respond_to do |format|
format.js if request.xhr?
format.html {redirect_to_index}
end
end
end
--------------------------- (cart_item.rb)
def initialize(product, quantity)
@product = product
@quantity = quantity
end
def increment_quantity(quantity)
@quantity += quantity
end
def price
#### product has a p200 field with the price in it
@product.p200 * @quantity
end
-----------------------------(cart.rb)
def add_product(product, quantity)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity(quantity)
else
current_item = CartItem.new(product, quantity)
@items << current_item
end
current_item
end
def total_price
@items.sum { |item| item.price }
end
def total_items
@items.sum { |item| item.quantity }
end
The code you provided in your form:
Qty : <%= text_field_tag ''quantity'', ''1'',
:size => 2, :maxlength => 2
%>
<% form_remote_tag :url => {:action => ''add_to_cart'',
:id =>
product }
do %>
<%= submit_tag "Add to Cart" %>
<% end %>
You need to have the quantity field within the form:
Qty :
<% form_remote_tag :url => {:action => ''add_to_cart'',
:id =>
product }
do %>
<%= text_field_tag ''quantity'', ''1'', :size
=> 2, :maxlength => 2
%>
<%= submit_tag "Add to Cart" %>
<% end %>
On Jun 17, 4:17 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> hi all..
> I''ve been studying ROR using the agile development ROR book.
> and I''m stuck at the shopping cart.
>
> what I am trying to do is that, from the book, the
''add_to_cart''
> button doesn''t take the quantity from the users.
>
> well, What I am trying to do is that allowing users to input quantity
> value
> before pressing ''add_to_cart'' and when
''add_to_cart'' is pressed, the
> users can see
> the input values with the total price before checking out the items.
>
> So, on the view, I added the following line.
>
> Qty : <%= text_field_tag ''quantity'',
''1'', :size => 2, :maxlength => 2
> %>
>
> <% form_remote_tag :url => {:action =>
''add_to_cart'', :id => product }
> do %>
> <%= submit_tag "Add to Cart" %>
> <% end %>
>
> and the store_controller,
>
> quantity = Integer(params[''quantity''])
>
> well..
> the flashing cart shows 0 quantity.
> i need help, please?
>
> ///////////////
> Your Cart
> 0(quantity) × 1(prod_id) $0.00
> Total $0.00
> ////////////////
>
> --------------------------- (controller)
> def add_to_cart
> begin
> product = Product.find(params[:id])
> quantity = Integer(params[''quantity'']) #------ i hope
this is
> right way to get quantity from the view
>
> rescue ActiveRecord::RecordNotFound
> logger.error("Attempt to access invalid product
#{params[:id]}" )
> redirect_to_index("Invalid product" )
> else
> @cart = find_cart
> @current_item = @cart.add_product(product, quantity)
> respond_to do |format|
> format.js if request.xhr?
> format.html {redirect_to_index}
> end
> end
> end
>
> --------------------------- (cart_item.rb)
> def initialize(product, quantity)
> @product = product
> @quantity = quantity
> end
> def increment_quantity(quantity)
> @quantity += quantity
> end
> def price
>
> #### product has a p200 field with the price in it
> @product.p200 * @quantity
> end
>
> -----------------------------(cart.rb)
> def add_product(product, quantity)
> current_item = @items.find {|item| item.product == product}
> if current_item
> current_item.increment_quantity(quantity)
> else
> current_item = CartItem.new(product, quantity)
> @items << current_item
> end
> current_item
> end
>
> def total_price
> @items.sum { |item| item.price }
> end
>
> def total_items
> -pbaaTb+XPjAAvxtiuMwx3w@public.gmane.org { |item|
item.quantity }
> end
^^ Henry.. thanx ..for checking my codes.. and pointing out the error.. it''s working now.. :) On 6월17일, 오후11시17분, Nicholas Henry <nicholas.he...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The code you provided in your form: > > Qty : <%= text_field_tag ''quantity'', ''1'', :size => 2, :maxlength => 2 > %> > <% form_remote_tag :url => {:action => ''add_to_cart'', :id => > product } > do %> > <%= submit_tag "Add to Cart" %> > <% end %> > > You need to have the quantity field within the form: > > Qty : > <% form_remote_tag :url => {:action => ''add_to_cart'', :id => > product } > do %> > <%= text_field_tag ''quantity'', ''1'', :size => 2, :maxlength => 2 > %> > <%= submit_tag "Add to Cart" %> > <% end %> > > On Jun 17, 4:17 am, Mike75 <youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > hi all.. > > I''ve been studying ROR using the agile development ROR book. > > and I''m stuck at the shopping cart. > > > what I am trying to do is that, from the book, the ''add_to_cart'' > > button doesn''t take the quantity from the users. > > > well, What I am trying to do is that allowing users to input quantity > > value > > before pressing ''add_to_cart'' and when ''add_to_cart'' is pressed, the > > users can see > > the input values with the total price before checking out the items. > > > So, on the view, I added the following line. > > > Qty : <%= text_field_tag ''quantity'', ''1'', :size => 2, :maxlength => 2 > > %> > > > <% form_remote_tag :url => {:action => ''add_to_cart'', :id => product } > > do %> > > <%= submit_tag "Add to Cart" %> > > <% end %> > > > and the store_controller, > > > quantity = Integer(params[''quantity'']) > > > well.. > > the flashing cart shows 0 quantity. > > i need help, please? > > > /////////////// > > Your Cart > > 0(quantity) × 1(prod_id) $0.00 > > Total $0.00 > > //////////////// > > > --------------------------- (controller) > > def add_to_cart > > begin > > product = Product.find(params[:id]) > > quantity = Integer(params[''quantity'']) #------ i hope this is > > right way to get quantity from the view > > > rescue ActiveRecord::RecordNotFound > > logger.error("Attempt to access invalid product #{params[:id]}" ) > > redirect_to_index("Invalid product" ) > > else > > @cart = find_cart > > @current_item = @cart.add_product(product, quantity) > > respond_to do |format| > > format.js if request.xhr? > > format.html {redirect_to_index} > > end > > end > > end > > > --------------------------- (cart_item.rb) > > def initialize(product, quantity) > > @product = product > > @quantity = quantity > > end > > def increment_quantity(quantity) > > @quantity += quantity > > end > > def price > > > #### product has a p200 field with the price in it > > @product.p200 * @quantity > > end > > > -----------------------------(cart.rb) > > def add_product(product, quantity) > > current_item = @items.find {|item| item.product == product} > > if current_item > > current_item.increment_quantity(quantity) > > else > > current_item = CartItem.new(product, quantity) > > @items << current_item > > end > > current_item > > end > > > def total_price > > @items.sum { |item| item.price } > > end > > > def total_items > > @items.sum { |item| item.quantity } > > end