JQuery 无法与我的 Rails 3.2 项目一起启动/工作

发布于 2025-01-06 13:05:48 字数 2817 浏览 1 评论 0原文

正如标题所示,我已确保所有需要的 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)

donations/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 %>

As in the title, I`ve made sure all needed JS files are included in the meta tags, included the jquery-rails/coffee-rails gem, linked to a external one just in case.

This is a modified script from Ryan Bates tutorial on Stripe, from what I understand it`s suppose to fire up by detecting the submit action from the form.

Since I'm not that familiar with JS or CoffeeScript, I can't really pin point what's the problem here.

Would appreciate any help I can get.

The donations.js file:

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)

The donations/new.html.erb form

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2025-01-13 13:05:48

您的 javascript 似乎是,Coffeescript - 确保将其放入自己的 donations.js.coffee 文件中,并且您包含 coffee- Rails gem,当你在做的时候。

标准调试技术在这里会很有用 - 尝试在初始 jQuery -> 行之后添加 alert("It live!") 或其他内容,以查看 document.ready 是否已就绪处理程序一直被调用。

如果没有,请检查以确保首先加载 donations.js.coffee 文件,如果没有,请确保您将其加载到页面的标头中,或者您已经在 application.js 中是这样的:

//= require jquery
//= require donations

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

//= require jquery
//= require_tree .

Your javascript seems to be, well, Coffeescript - make sure it's put in its own donations.js.coffee file, and that you're including the coffee-rails gem while you're at it.

Standard debugging techniques would be useful here - try adding an alert("It lives!") or something after the initial jQuery -> line to see if the document.ready handler is ever being called.

If not, check to make sure that the donations.js.coffee file is being loaded in the first place, and if not, make sure you're either loading it in your page's header, or you have something like this in application.js:

//= require jquery
//= require donations

Or, if you just want to include everything in app/assets/javascripts:

//= require jquery
//= require_tree .
茶花眉 2025-01-13 13:05:48

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

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

Peter is correct, your coffeescript code should be properly named so that it can be translated to JavaScript.

An additional debugging strategy is to look at the html/js source being sent to your browser. Use the view source command to see the html source. Then click on the reference to the js link containing the translated donations.js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文