【问题标题】:ExecJS::RuntimeError in Listings#index error [closed]Listings#index 错误中的 ExecJS::RuntimeError [关闭]
【发布时间】:2014-10-27 10:15:22
【问题描述】:

我在打开本地主机时收到此 ExecJS 错误消息,我不知道为什么,如果有帮助会很棒。

我在本地主机上得到了这个

显示 /.../conektec/app/views/layouts/application.html.erb 第 6 行出现的位置:

SyntaxError: [stdin]:6:16: 意外换行 (在 /.../conektec/app/assets/javascripts/orders.js.coffee)

  ActionView::Template::Error (SyntaxError: [stdin]:2:73: unmatched )
  (in /Users/hbendev/startups/conektec/conektec/app/assets/javascripts/orders.js.coffee)):
    3: <head>
    4:   <title>Conektec</title>
    5:   <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    6:   <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    7:   <%= javascript_include_tag "https://js.stripe.com/v2/" %> 
    8:   <%= csrf_meta_tags %>
    9:   <%= tag :meta, :name => "stripe-key", :content => ENV["STRIPE_PUBLIC_KEY"] %>

这是我的 orders.js.coffee 文件

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  payment.setupForm()

payment =
  setupForm: ->
  $('#new_order').submit ->
    $('input[type=submit]').attr('disabled', true)
    Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
    false

handleStripeResponse: (status, response) ->
  if status == 200
    alert(response.id)
  else
    alert(response.error.message)

这是我的 application.html.erb 文件

<!DOCTYPE html>
<html>
<head>
  <title>Conektec</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag "https://js.stripe.com/v2/" %> 
  <%= csrf_meta_tags %>
  <%= tag :meta, :name => "stripe-key", :content => ENV["STRIPE_PUBLIC_KEY"] %>
</head>

<body>

<%= render 'layouts/header' %>

<div class="container">

    <% flash.each do |name, msg| %>
        <% if msg.is_a?(String) %>
            <div class="alert alert-<%= name.to_s == 'notice' ? "success" : "danger" %> alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <%= content_tag :div, msg, :id => "flash_#{name}" %>
            </div>
        <% end %>
    <% end %>

    <%= yield %>

    <%= render 'layouts/footer' %>
</div>


</body>
</html>

我已经尝试删除我的 turbolinks,添加 rubyracer gem,我已经安装了 nodejs,我不知道错误在哪里。

我正在使用: OS X 小牛队 Ruby 2.0.0 Rails 4.1.1

怎么了? 谢谢

【问题讨论】:

  • 这是你 CoffeeScript 中真正的缩进吗?
  • 你认为Stripe.setPublishableKey($)('meta[name="stripe-key"]')会做什么?
  • 哦,我刚刚意识到是缩进,真的。我修复它。现在错误消失了,但又遇到了另一个问题,当我点击提交按钮时(请参阅咖啡脚本代码)我在浏览器(Chrome)中没有收到任何弹出消息,唯一的操作是 .attr('disabled ', true) 在我单击它后禁用该按钮。我将Stripe.setPublishableKey($)('meta[name="stripe-key"]').attr('content'))的代码编辑为:Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')),同样的错误,没有出现警告。

标签: javascript ruby-on-rails ruby node.js coffeescript


【解决方案1】:

您的问题在于您的 CoffeeScript 语法(第 6 行,第 16 列,如错误中所述):

# The lack of indentation after the setupForm line is incorrect
payment =
  setupForm: ->
  $('#new_order').submit ->
    $('input[type=submit]').attr('disabled', true)
    Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
    false

# Make it this
payment = 
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
      false

编辑:值得注意的是,每当出现 ExecJS 错误时,这通常是一个很好的迹象,表明您的 CoffeeScript 语法存在问题(从而导致编译错误)。在这种情况下,这不是真正的 JavaScript 错误。'

要解决您遇到的其他问题,由于我无法发表评论,您需要缩进您的 handleStripeResponse 函数,以便它嵌套在您的付款对象下方:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  payment.setupForm()

payment =
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
      false

  handleStripeResponse: (status, response) ->
    if status == 200
      alert(response.id)
    else
      alert(response.error.message)

我不确定您是如何遇到这些错误的,因为它们是直接从 Railscasts 剧集中复制而来的;尽量小心使用 CoffeeScript 的缩进。

【讨论】:

  • 感谢 SO 大厅监视器再次关闭一个有用的问题,让他们成为房间里最聪明的人。
  • @user1130176 不确定你的意思——我不是结束这个问题的人。我差不多 3 年前就回答过了。
  • 我只是在发泄,我非常讨厌人们在为他人提供价值时将如此密切的问题视为题外话。这只是幼稚的自负,不为任何人服务。
猜你喜欢
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 2016-09-13
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2017-02-10
相关资源
最近更新 更多