Trying to merge two different forms into one has proven to be quite challenging for me.
The recommendation I received was to move the create method from ChargesController to OrderController, but it's not as simple as that. The Charges Form requires javascript in order for the token to be sent via API with the "id="payment_form". So now I'm exploring options to combine both forms using either Ruby syntax or potentially Javascript.
My ideal scenario would involve the following logic...
If the Charges form submission is successful, then proceed to submit the Orders Form.
<form id="form-element" action="/charges" method="post" id="payment_form">
<%= form_for([@listing, @order]) do |form| %>
...
I've been unsuccessful in merging the top two lines together thus far. Any tips or suggestions?
I'm tagging stripe and javascript for anyone wondering because I believe that javascript might hold the key to solving this issue, especially for those who have experience working with Stripe.
Javascript for the Stripe elements Form:
<script>
// Code snippet for initializing Stripe
</script>
Update:
Implemented the following changes:
<%= form_for([@listing, @order], html: {id: "Orders"}) do |form| %>
Integrated this piece of code into the JavaScript section:
$('#Orders').on('submit', function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "/charges",
data: $('#payment_form').serialize()
}).then(this.submit.bind(this));
});