Created
August 17, 2013 15:44
-
-
Save stijlist/6257510 to your computer and use it in GitHub Desktop.
Trying to understand why my submit handlers don't fire in the new action of a default rails 4 controller.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Charge $10 with Stripe</h1> | |
<form action="" method="POST" id="payment-form"> | |
<span class="payment-errors"></span> | |
<div class="form-row"> | |
<label> | |
<span>Card Number</span> | |
<input type="text" size="20" data-stripe="number"/> | |
</label> | |
</div> | |
<div class="form-row"> | |
<label> | |
<span>CVC</span> | |
<input type="text" size="4" data-stripe="cvc"/> | |
</label> | |
</div> | |
<div class="form-row"> | |
<label> | |
<span>Expiration (MM/YYYY)</span> | |
<input type="text" size="2" data-stripe="exp-month"/> | |
</label> | |
<span> / </span> | |
<input type="text" size="4" data-stripe="exp-year"/> | |
</div> | |
<button type="submit">Submit Payment</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Stripetest</title> | |
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> | |
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> | |
<%= csrf_meta_tags %> | |
<title>Stripe Getting Started Form</title> | |
<!-- The required Stripe lib --> | |
<script type="text/javascript" src="https://js.stripe.com/v1/"></script> | |
<!-- jQuery is used only for this example; it isn't required to use Stripe --> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
// This identifies your website in the createToken call below | |
Stripe.setPublishableKey('pk_test_kPzuqE3wLFBn1MMbjYVSMQ0P'); | |
// testCard = { | |
// card_number: 4242424242424242, | |
// cvc: 222, | |
// expMonth: 1, | |
// expYear: 15 | |
// } | |
var stripeResponseHandler = function(status, response) { | |
var $form = $('#payment-form'); | |
if (response.error) { | |
// Show the errors on the form | |
$form.find('.payment-errors').text(response.error.message); | |
$form.find('button').prop('disabled', false); | |
} else { | |
// token contains id, last4, and card type | |
var token = response.id; | |
alert("Token is " + token); | |
// Insert the token into the form so it gets submitted to the server | |
$form.append($('<input type="hidden" name="stripeToken" />').val(token)); | |
// and re-submit | |
$form.get(0).submit(); | |
} | |
}; | |
jQuery(function($) { | |
$('#payment-form').submit(function(e) { | |
var $form = $(this); | |
// Disable the submit button to prevent repeated clicks | |
$form.find('button').prop('disabled', true); | |
Stripe.createToken($form, stripeResponseHandler); | |
// Prevent the form from submitting with the default action | |
return false; | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StripePaymentsController < ApplicationController | |
before_action :set_stripe_payment, only: [:show, :edit, :update, :destroy] | |
# GET /stripe_payments | |
# GET /stripe_payments.json | |
def index | |
@stripe_payments = StripePayment.all | |
end | |
# GET /stripe_payments/1 | |
# GET /stripe_payments/1.json | |
def show | |
end | |
# GET /stripe_payments/new | |
def new | |
@stripe_payment = StripePayment.new | |
end | |
# GET /stripe_payments/1/edit | |
def edit | |
end | |
# POST /stripe_payments | |
# POST /stripe_payments.json | |
def create | |
@stripe_payment = StripePayment.new(stripe_payment_params) | |
respond_to do |format| | |
if @stripe_payment.save | |
format.html { redirect_to @stripe_payment, notice: 'Stripe payment was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @stripe_payment } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @stripe_payment.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /stripe_payments/1 | |
# PATCH/PUT /stripe_payments/1.json | |
def update | |
respond_to do |format| | |
if @stripe_payment.update(stripe_payment_params) | |
format.html { redirect_to @stripe_payment, notice: 'Stripe payment was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @stripe_payment.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /stripe_payments/1 | |
# DELETE /stripe_payments/1.json | |
def destroy | |
@stripe_payment.destroy | |
respond_to do |format| | |
format.html { redirect_to stripe_payments_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_stripe_payment | |
@stripe_payment = StripePayment.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def stripe_payment_params | |
params[:stripe_payment] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment