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 -- Posted via http://www.ruby-forum.com/.
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" <dokie1963-misc@yahoo.co.uk> To: <rails@lists.rubyonrails.org> Sent: 2006-04-11 12:36 PM Subject: [Rails] 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Bill Walton 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" <dokie1963-misc@yahoo.co.uk> > To: <rails@lists.rubyonrails.org> > Sent: 2006-04-11 12:36 PM > Subject: [Rails] Extending Depot ExampleBill 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"> </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 -- Posted via http://www.ruby-forum.com/.