0

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1429496 I made a bit of a typo. Check it again for a correct condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.