我正在使用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")
未被记录。
我在我的应用程序的其他部分使用coffeescript,我也在其他地方使用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
知道是什么导致了这个问题和/或如何修复它?谢谢!
答案 0 :(得分:0)
我能够通过将<%= javascript_include_tag "application" %>
从<body>
中application.html.erb
的底部移到顶部来解决此问题,以便它在<%= yield %>
之前运行,这样jQuery在视图之前加载。