【问题标题】:Rails 4 ExecJS::ProgramError in Listings#indexRails 4 ExecJS::ProgramError in Listings#index
【发布时间】:2014-06-25 20:15:04
【问题描述】:

将 JavaScript 添加到我的网络应用程序后,我现在收到以下错误:

ExecJS::ProgramError in Listings#index

[stdin]:16:5: error: unexpected else
    else
    ^^^^
  (in /Users/Desktop/Rails/foodapp/app/assets/javascripts/orders.js.coffee)

application.html.etb

<!DOCTYPE html>
<html>
<head>
  <title>Foodapp</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 %>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <%= 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 == :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 %>

orders.js.cofee

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
      $('#new_order').append($('<input type="hidden" name="stripeToken" />').val(response.id))
      $('#new_order')[0].submit()
    else
      $('#stripe_error').text(response.error.message).show()
      $('input[type=submit]').attr('disabled', false)

listing.index.html.erb

<h2>All products</h2>

<table class="table table-striped">
  <thead>
    <tr>
      <th class="center">Name</th>
      <th class="center">Description</th>
      <th class="center">Stock</th>
      <th class="center">Price</th>
      <th class="center">Sold by</th>
    </tr>
  </thead>

  <tbody>
    <% @listings.each do |listing| %>
      <tr>
        <td><%= listing.name %></td>
        <td><%= listing.description %></td>
        <td><%= listing.stock %></td>
        <td><%= number_to_currency(listing.price) %></td>
        <td><%= listing.user.name %></td>
        <%= link_to 'Buy Now', listing, class: "btn btn-link" %>

      </tr>

    <% end %>
  </tbody>
</table>

<br>

<% if user_signed_in? %>
<%= link_to 'New Listing', new_listing_path, class: "btn btn-link" %>
<% end %>

listings.show

<p>
  <strong>Name:</strong>
  <%= @listing.name %>
</p>

<p>
  <strong>Description:</strong>
  <%= @listing.description %>
</p>

<p>
  <strong>Stock:</strong>
  <%= @listing.stock %>
</p>

<p>
  <strong>Price:</strong>
  <%= number_to_currency(@listing.price) %>
</p>

<%= link_to "Buy It Now", new_listing_order_path(@listing), class: "btn btn-primary", data: { no_turbolink: true } %>

<% if current_user == @listing.user %>
    <%= link_to 'Edit', edit_listing_path(@listing), class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', listings_path, class: "btn btn-link" %>

宝石文件

来源'https://rubygems.org'
源'https://code.stripe.com'

ruby "2.0.0"
gem 'rails', '4.0.4'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass'
gem 'devise'
gem "figaro"
gem 'stripe'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

【问题讨论】:

  • 这是一个很奇怪的bug。何时粘贴您的代码我没有收到任何错误。在这种情况下,我通常会删除可能出错的代码,并尝试查找错误;)
  • 是的,我认为这可能是购买按钮后添加的 turbolinks 代码
  • 这种类型的错误意味着coffeescript预处理器无法将你的coffee编译为js,这意味着你的coffee代码中有错误。我确定不会因为购买按钮上的数据属性而引发错误

标签: javascript ruby-on-rails ruby


【解决方案1】:

我也遇到过同样的问题。
我猜这个错误是由第 16 行的文本缩进引起的。

我正在使用 Sublime 3 编辑器。 我的解决方案非常简单:
1. 修改编辑器窗口右下角tab with = 2属性。
2.选择同侧Convert Indentation to Spaces

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2015-04-09
    • 2016-06-22
    相关资源
    最近更新 更多