Question

I am using the Koudoku gem to integrate Stripe into my rails app, and when I load the page that captures the users credit card information, the Stripe javascript should run, however, my JS console shows the following error:

Uncaught ReferenceError: $ is not defined
(anonymous function)

This is the JS provided by Koudoku/Stripe that should run when the page loads:

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

Needless to say, the console.log("testing log") is not being logged.

I am using coffeescript in other parts of my app, and I am using jQuery elsewhere as well. Here is my 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

Any idea what is causing this issue and/or how to fix it? Thanks!

Was it helpful?

Solution

I was able to fix this by moving <%= javascript_include_tag "application" %> from the bottom of the <body> in application.html.erb to the top so that it was run before the <%= yield %>, this way jQuery was loaded before the views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top