Extending Depot Example

Hi

I am new to the whole Rails/Ruby world as having come from a MS .NET
background, but I have followed the Depot example in the Agile Web Dev.
with Rails book with relative ease. However as an exercise to see how
much I had understood, I thought I would attempt to modify the example a
bit and add a Quantity field to the store index so as to pass that value
into the cart. I have extended the cart behaviour OK but I am having
great difficulty seeing how to get this quantity value from the index
view into the controller’s add_to_cart behaviour. I have tried various
approaches but none of them seem to work. Has anyone else done this and
if so how? If not, then how should that example view be modified
(preferably without modifying any model classes) to facilitate this
requirement?

Thanks in advance
Mike

Hi Mike,

I’ve found it’s easier to get help here when I post something concrete.
My
recommendation is to post a bit of your code so we can see where you’re
having trouble.

Best regards,
Bill

----- Original Message -----
From: “Mike Williams” [email protected]
To: [email protected]
Sent: 2006-04-11 12:36 PM
Subject: [Rails] Extending Depot Example

Bill W. wrote:

Hi Mike,

I’ve found it’s easier to get help here when I post something concrete.
My
recommendation is to post a bit of your code so we can see where you’re
having trouble.

Best regards,
Bill

----- Original Message -----
From: “Mike Williams” [email protected]
To: [email protected]
Sent: 2006-04-11 12:36 PM
Subject: [Rails] Extending Depot Example

Bill

OK. The example view (index.rhtml) is as follows

<% for product in @products %>



<%= h(product.title) %>


<%= product.description %>
<%= number_to_currency(product.price)
%>

<%= link_to ‘Add to Cart’,
{:action => ‘add_to_cart’, :id => product},
:class => ‘addtocart’ %>


 

<% end %>
<%= link_to “Show my cart”, :action => “display_cart” %>

I would like to add a quantity value to this view for each product so
that when the user adds it to the cart, this extra piece of data is
passed. The Product class would not really support the quantity value,
so how can this view be modified? The Controller code is

def add_to_cart
product_id = params[:id]
product = Product.find(product_id)
@cart = find_cart
@cart.add_product(product, params[:qty])
redirect_to(:action => ‘display_cart’)
rescue
logger.error(“Attempt to access invalid product #{params[:id]}”)
redirect_to_index(‘Invalid product’)
end

where I have coded the parameter :qty in anticipation of being able to
pass it in this manner. From the action onwards into the cart, the code
is fine, it is just the view I cannot get my head around.

Thanks
Mike

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs