【问题标题】:Javascript error in rails app when integrating Stripe API集成 Stripe API 时 Rails 应用程序中的 Javascript 错误
【发布时间】:2014-04-21 18:02:24
【问题描述】:

我正在使用 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

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

【问题讨论】:

  • 是什么导致 jQuery 被导入?失败意味着答案是“否”,或者它正在被导入,这样 jQuery 就会出现在 该代码之后。
  • @Pointy,那时我可以显式导入 jQuery 吗?如果我这样做了,如果以后再导入它会搞砸吗?
  • 如果gem 行的排序与结果排序有关,那么无论如何我都会将 jQuery 之类的东西放在顶部。

标签: javascript jquery ruby-on-rails coffeescript stripe-payments


【解决方案1】:

我可以通过将&lt;%= javascript_include_tag "application" %&gt;application.html.erb 中的&lt;body&gt; 底部移动到顶部来解决此问题,以便它在&lt;%= yield %&gt; 之前运行,这样jQuery 在视图之前加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多