Forum: Ruby on Rails Extending Depot Example

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike W. (Guest)
on 2006-04-11 21:36
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
Bill W. (Guest)
on 2006-04-11 21:56
(Received via mailing list)
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" <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
Sent: 2006-04-11 12:36 PM
Subject: [Rails] Extending Depot Example
Mike W. (Guest)
on 2006-04-12 01:57
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" <removed_email_address@domain.invalid>
> To: <removed_email_address@domain.invalid>
> 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 %>
    <div class="catalogentry">
        <img src="<%= product.image_url %>"/>
        <h3><%= h(product.title) %></h3>
        <%= product.description %>
        <span class="catalogprice"><%= number_to_currency(product.price)
%></span>
        <%= link_to 'Add to Cart',
                    {:action => 'add_to_cart', :id => product},
                    :class => 'addtocart'  %><br/>
    </div>
    <div class="separator">&nbsp;</div>
<% 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 topic is locked and can not be replied to.