我在Enrollment#new上有一个条带形式的注册模型。当用户提交表单时,它似乎是两次向Enrollment#create。
提交这是带有条纹形式的new.html.erb:
<%= form_tag enrollments_path, :id=>"stripeForm" do %>
<%= text_field_tag(:number, nil, :placeholder=>"Credit Card Number", :class=>"form-control", :maxlength => 19, :'data-stripe'=>"number") %>
<%= text_field_tag(:'exp-month', nil, :placeholder=>"mm", :class=>"form-control", :maxlength => 2, :'data-stripe'=>"exp-month") %>
<%= text_field_tag(:'exp-year', nil, :placeholder=>"yy", :class=>"form-control", :maxlength => 2, :'data-stripe'=>"exp-year") %>
<%= text_field_tag(:cvc, nil, :placeholder=>"cvc", :class=>"form-control", :maxlength => 3, :'data-stripe'=>"cvc") %>
<%= hidden_field_tag 'stripeAmount', @workshop.price %>
<%= hidden_field_tag 'workshop', @workshop.id %>
<button class="buy-button col-xs-12 col-md-6 col-md-offset-3" id="stripe-button">Register Now</button>
<% end %>
<script>
var ready;
ready = function() {
Stripe.setPublishableKey("<%= ENV['stripe_publishable_key'] %>");
var stripeResponseHandler = function(status, response) {
var $form = $('#stripeForm');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').show();
$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;
// 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();
}
};
$(function() {
$('#stripeForm').submit(function(e) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
};
$(document).ready(ready);
$(document).on('page:load', ready);
</script>
这里是控制器操作(使用puts语句进行调试):
def new
@workshop = Workshop.find(params[:workshop])
@enrollment = Enrollment.new
end
def create
# Amount in cents
amount = params[:stripeAmount].to_i * 100
puts current_user.email
# Create the customer in Stripe
customer = Stripe::Customer.create(
email: current_user.email,
card: params[:stripeToken]
)
puts "&&&&&&&&&customer created&&&&&&&&&"
# Create the charge using the customer data returned by Stripe API
charge = Stripe::Charge.create(
customer: customer.id,
amount: amount,
description: 'Rails Stripe customer',
currency: 'usd'
)
puts "&&&&&&&&&charge created&&&&&&&&&"
@workshop = params[:workshop]
if charge["paid"] == true
@enrollment = Enrollment.new(user_id: current_user.id, workshop_id: @workshop)
puts "&&&&&&&&& enrollment new created &&&&&&&&&"
if @enrollment.save
puts "&&&&&&&&& enrollment saved &&&&&&&&&"
redirect_to thank_you_path + "?workshop=" + @workshop
else
flash[:notice] = "Please try again"
end
end
# place more code upon successfully creating the charge
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to workshop_path(@workshop)
flash[:notice] = "Please try again"
end
提交表单后立即显示日志:
Started POST "/enrollments" for ::1 at 2015-08-20 16:07:28 -0700
Processing by EnrollmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<redacted>", "number"=>"4242424242424242", "exp-month"=>"12", "exp-year"=>"16", "cvc"=>"123", "stripeAmount"=>"150", "workshop"=>"2", "stripeToken"=>"<redacted>"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
<redacted>
&&&&&&&&&customer created&&&&&&&&&
&&&&&&&&&charge created&&&&&&&&&
&&&&&&&&& enrollment new created &&&&&&&&&
(0.2ms) begin transaction
SQL (0.5ms) INSERT INTO "enrollments" ("user_id", "workshop_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 1], ["workshop_id", 2], ["created_at", "2015-08-20 23:07:29.937242"], ["updated_at", "2015-08-20 23:07:29.937242"]]
############### NEW ENROLLMENT CREATED 25 #######################
yay
Rendered enrollment_mailer/notification_email.html.erb within layouts/mailer (0.4ms)
EnrollmentMailer#notification_email: processed outbound mail in 235.2ms
Sent mail to <redacted>@gmail.com (75.2ms)
Date: Thu, 20 Aug 2015 16:07:30 -0700
From: noreply@<redacted>.com
To: <redacted>@gmail.com
Message-ID: <55d65db22ebf2_136623fc6e02c5dd033956@<redacted>>
Subject: new registration on <redacted>
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<h1>New Email signup</h1>
</body>
</html>
</body>
</html>
&&&&& email sent &&&&&
(2.6ms) commit transaction
&&&&&&&&& enrollment saved &&&&&&&&&
Redirected to http://localhost:3000/thank-you?workshop=2
Completed 302 Found in 2245ms (ActiveRecord: 3.3ms)
Started POST "/enrollments" for ::1 at 2015-08-20 16:07:30 -0700
Processing by EnrollmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<redacted>", "number"=>"4242424242424242", "exp-month"=>"12", "exp-year"=>"16", "cvc"=>"123", "stripeAmount"=>"150", "workshop"=>"2", "stripeToken"=>"<redacted>"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
<redacted>@gmail.com
&&&&&&&&&customer created&&&&&&&&&
&&&&&&&&&charge created&&&&&&&&&
&&&&&&&&& enrollment new created &&&&&&&&&
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "enrollments" ("user_id", "workshop_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 1], ["workshop_id", 2], ["created_at", "2015-08-20 23:07:31.710257"], ["updated_at", "2015-08-20 23:07:31.710257"]]
############### NEW ENROLLMENT CREATED 26 #######################
yay
Rendered enrollment_mailer/notification_email.html.erb within layouts/mailer (0.1ms)
EnrollmentMailer#notification_email: processed outbound mail in 5.6ms
Sent mail to <redacted>@gmail.com (12.2ms)
Date: Thu, 20 Aug 2015 16:07:31 -0700
From: noreply@<redacted>.com
To: <redacted>@gmail.com
Message-ID: <55d65db3af982_136623fc6e1cd26603403f@<redacted>-MacBook-Pro.local.mail>
Subject: new registration on <redacted>
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<h1>New Email signup</h1>
</body>
</html>
</body>
</html>
&&&&& email sent &&&&&
(1.6ms) commit transaction
&&&&&&&&& enrollment saved &&&&&&&&&
Redirected to http://localhost:3000/thank-you?workshop=2
Completed 302 Found in 1457ms (ActiveRecord: 2.2ms)
Started GET "/thank-you?workshop=2" for ::1 at 2015-08-20 16:07:31 -0700
Processing by EnrollmentsController#thanks as HTML
Parameters: {"workshop"=>"2"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Workshop Load (0.1ms) SELECT "workshops".* FROM "workshops" WHERE "workshops"."id" = ? LIMIT 1 [["id", 2]]
Rendered enrollments/thanks.html.erb within layouts/application (4.3ms)
Completed 200 OK in 652ms (Views: 649.0ms | ActiveRecord: 0.2ms)
答案 0 :(得分:1)
想出来。与使用以下代码提交表单加载两次的javascript有关:
$(document).ready(ready);
$(document).on('page:load', ready);
刚刚删除了那个和现成的var,它都被修复了......非常令人困惑
答案 1 :(得分:0)
您是否使用任何验证插件,例如 jquery.validate? 确保您拥有:
$form.submit(function(event) {
event.stopImmediatePropagation();
}
因为这里的验证插件在成功验证所有字段后也会提交表单。 您也可以尝试使用 submitHandler() 代替 .submit()