JQuery无法启动我的Rails 3.2项目

JQuery doesn`t fire/work with my Rails 3.2 project

本文关键字:Rails 2项目 我的 启动 JQuery      更新时间:2023-09-26

在标题中,我已经确保所有需要的JS文件都包含在元标记中,包括jquery rails/coffee rails gem,并链接到外部文件以备不时之需。

这是Ryan Bates关于Stripe教程中的一个修改过的脚本,据我所知,它应该通过检测表单中的提交动作来启动

由于我对JS或CoffeeScript不太熟悉,所以我无法真正指出这里的问题所在。

如果我能得到任何帮助,我将不胜感激。

donations.js文件:

jQuery ->
      Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
      donation.setupForm()
donation =
  setupForm: ->
    $('#new_donation').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        donation.processCard()
        false
      else
        true
  processCard: ->
      card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, donation.handleStripeResponse)
  handleStripeResponse: (status, response) ->
    if status == 200
      alert(response.id)
    else
      alert(response.error.message)

捐款/new.html.erb表格

<%= form_for(@donation) do |f| %>
                <div class="field">
                    <%= f.label :from %>
                    <%= f.text_field :from %>
                </div>
                <div class="field">
                  <%= label_tag :card_number, "Credit Card Number" %>
                  <%= text_field_tag :card_number, nil, :name => nil %>
                </div>
                <div class="field">
                  <%= label_tag :card_code, "Security Code (CVV)" %>
                  <%= text_field_tag :card_code, nil, :name => nil %>
                </div>
                <div class="field">
                  <%= label_tag :card_month, "Card Expiration" %>
                  <%= select_month nil, {:add_month_numbers => true}, {:name => nil, :id => "card_month"} %>
                  <%= select_year nil, {:start_year => Date.today.year, :end_year => Date.today.year+15}, {:name => nil, :id => "card_year"} %>
                </div>
                <div class="field">
                    <%= f.label :Notes %>
                    <%= f.text_field :note %>
                </div>
                <br>
                <center>
                    <span class="BigBold">Amount: $2.50</span>
                    <%= f.hidden_field :amount, {:value => "2,5" } %><br>
                </center>
                <br>
                <center>
                    <%= f.submit "Donate Now", :class => 'donate-button' %>
                </center>
            <% end %>

您的javascript似乎是,嗯,Coffeescrapt-确保它被放在自己的donations.js.coffee文件中,并且在使用时包含了coffee-rails gem。

标准调试技术在这里会很有用——试着在最初的jQuery ->行后面添加一个alert("It lives!")或其他东西,看看是否调用了document.ready处理程序。

如果没有,请检查以确保首先加载了donations.js.coffee文件,如果没有,则确保您正在将其加载到页面的页眉中,或者您在application.js:中有类似的内容

//= require jquery
//= require donations

或者,如果您只想在app/assets/javascripts:中包含所有内容

//= require jquery
//= require_tree .

Peter是正确的,您的coffeescript代码应该正确命名,以便可以将其翻译为JavaScript。

另一种调试策略是查看发送到浏览器的html/js源代码。使用"查看源代码"命令可以查看html源代码。然后单击包含翻译捐赠.js的js链接的引用。