Question

I have a multi-step Rails 3 form that I set up using this railscast: http://railscasts.com/episodes/217-multistep-forms, and I used another railscast to implement Stripe payment for my app: http://railscasts.com/episodes/288-billing-with-stripe

In my app, the payment form is hidden via jQuery if the amount charged to the user is 0.

$(document).ready(function() {
  var xyz = $("span#total").text();

  if (xyz == 0){
    $("div#stripefields").hide();
  };    
});

What I want to be able to do is tell my Rails app that the Stripe payment fields are not required to successfully submit the form if the amount charged to the user is 0. So, I need to modify this statement in my controller to reflect that:

elsif @video.last_step?
   @video.save if @video.all_valid? && @video.save_with_payment(@total) 

@total is the amount charged to the user, and "save_with_payment" represents the Stripe charge. What would be the best way to go about doing this? Can I add another "if" to this statement somehow?

Was it helpful?

Solution

Edit: Sorry, I made a bit of a typo.

You need to check if the total charged is greater than 0 before paying, or allow the video to save if the total is 0.

@video.save if @video.all_valid? && ((@total > 0 && @video.save_with_payment(@total)) || @total == 0)

Also, in your jQuery code you're comparing an integer to a string. You may want to:

$(document).ready(function() {
  var xyz = parseFloat($("span#total").text());

  if (xyz === 0.0){
    $("div#stripefields").hide();
  }
});

Just to avoid potential inconsistencies. Comparing with == will attempt to coerce types, and you might get some odd results because of it. So by using parseFloat to turn whatever the total price is (either integer or float) into a float value and comparing against a float (0.0) we make the outcome quite clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top