Rails:条纹forms被发布两次

我在Enrollment#new上有一个条带forms的注册模型。 当用户提交表单时,它似乎是两次向Enrollment#create提交。

这是带条纹forms的new.html.erb:

"stripeForm" do %> "Credit Card Number", :class=>"form-control", :maxlength => 19, :'data-stripe'=>"number") %> "mm", :class=>"form-control", :maxlength => 2, :'data-stripe'=>"exp-month") %> "yy", :class=>"form-control", :maxlength => 2, :'data-stripe'=>"exp-year") %> "cvc", :class=>"form-control", :maxlength => 3, :'data-stripe'=>"cvc") %>      var ready; ready = function() { Stripe.setPublishableKey(""); 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($('').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);  

这是控制器动作(使用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"=>"", "number"=>"4242424242424242", "exp-month"=>"12", "exp-year"=>"16", "cvc"=>"123", "stripeAmount"=>"150", "workshop"=>"2", "stripeToken"=>""} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]  &&&&&&&&&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 @gmail.com (75.2ms) Date: Thu, 20 Aug 2015 16:07:30 -0700 From: noreply@.com To: @gmail.com Message-ID: <55d65db22ebf2_136623fc6e02c5dd033956@> Subject: new registration on  Mime-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit        

New Email signup

&&&&& 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"=>"", "number"=>"4242424242424242", "exp-month"=>"12", "exp-year"=>"16", "cvc"=>"123", "stripeAmount"=>"150", "workshop"=>"2", "stripeToken"=>""} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] @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 @gmail.com (12.2ms) Date: Thu, 20 Aug 2015 16:07:31 -0700 From: noreply@.com To: @gmail.com Message-ID: <55d65db3af982_136623fc6e1cd26603403f@-MacBook-Pro.local.mail> Subject: new registration on Mime-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

New Email signup

&&&&& 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)

弄清楚了。 与使用下面的代码提交两次加载表单的javascript有关:

  $(document).ready(ready); $(document).on('page:load', ready); 

刚删除它和准备好的var,它都是固定的..非常令人困惑