All threads / Rails + Javascript question

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails + Javascript question

Ignacio Alonso asked in Rails

Hey guys,

I have an app for generating orders containg products. I'm currently using a “cart” through table that helps me connect both orders and products. I would like to have a list of products where users can select what they want from the list via checkbox for e.g. and then add them to an array that would be sent to my controller but I don’t want to reload page on each product add from the list. What would be the best way to add the X number of products to the params after user ’adds' them to the cart and press send?

I would recommend accumulating the products and their assocaited quantities as objects inside a local JS array. When the end-user presses "send", you can catch that event and build/send a FormData() object via Rails.ajax. See my example over at JSFiddle @ https://jsfiddle.net/pupdogg/okagmx6p/8/ as I'm not sure how to embed it here. I hope this helps!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,095+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.