gpt4 book ai didi

集成 Stripe API 时 Rails 应用程序中出现 Javascript 错误

转载 作者:行者123 更新时间:2023-11-28 08:25:42 24 4
gpt4 key购买 nike

我正在使用 Koudoku gem 将 Stripe 集成到我的 Rails 应用程序中,当我加载捕获用户信用卡信息的页面时,Stripe javascript 应该运行,但是,我的 JS 控制台显示以下错误:

Uncaught ReferenceError: $ is not defined
(anonymous function)

这是 Koudoku/Stripe 提供的 JS,应该在页面加载时运行:

<script type="text/javascript">

// All this code taken from Stripe's own examples at:
// https://stripe.com/docs/tutorials/forms .

function stripeResponseHandler(status, response) {

if (response.error) {
// show the errors on the form
$(".payment-errors").text(response.error.message).show();
$(".submit-button").removeAttr("disabled");
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='subscription[credit_card_token]' value='" + response['id'] + "'/>");
form$.append("<input type='hidden' name='subscription[last_four]' value='" + response['last4'] + "'/>");
form$.append("<input type='hidden' name='subscription[card_type]' value='" + response['card_type'] + "'/>");
// and submit
form$.get(0).submit();
}
}

$(document).ready(function() {

console.log("testing log")

Stripe.setPublishableKey("<%= Koudoku.stripe_publishable_key %>");

// By default, don't show errors.
$(".payment-errors").hide()

$("#payment-form").submit(function(event) {

// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");

Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);

// prevent the form from submitting with the default action
return false;
});
});

</script>

不用说,console.log("testing log") 没有被记录。

我在应用程序的其他部分使用了coffescript,并且在其他地方也使用了jQuery。这是我的 Gemfile:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'

gem 'foundation-rails'
# gem 'chosen-rails'
gem 'select2-rails'

gem 'pg'

gem 'devise'
gem 'cancan'

gem 'state_machine'

gem 'koudoku'

group :development, :test do
gem 'rspec-rails'
# The following optional lines are part of the advanced setup.
gem 'guard-rspec'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork'
gem 'childprocess', '0.3.6'
end

group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'debugger'
end

group :test do
gem 'selenium-webdriver'
gem 'capybara'
gem 'factory_girl_rails'
# gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'

# gem 'simplecov', :require => false

# Uncomment this line on OS X.
gem 'growl', '1.0.3'

# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'

# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
end

gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
# gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
gem 'sdoc', '0.3.20', require: false
end

group :production do
gem 'rails_12factor', '0.0.2'
end

知道是什么导致了这个问题和/或如何解决它?谢谢!

最佳答案

我可以通过移动 <%= javascript_include_tag "application" %> 来解决这个问题从底部<body>application.html.erb到顶部,以便它在 <%= yield %> 之前运行,这样 jQuery 在 View 之前加载。

关于集成 Stripe API 时 Rails 应用程序中出现 Javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com