5

我正在app/assets/javascripts/ticket.js.coffee为特定视图运行 jQuery。每次我访问该页面时,浏览器都会呈现此错误 - 在线任何地方都没有提及此错误。

(本地主机:3000/Tickets/new)

SyntaxError: unexpected POST_IF

Extracted source (around line #6):

3: <head>
4:   <title>Ops2</title>
5:   <%= stylesheet_link_tag    "application", :media => "all" %>
6:   <%= javascript_include_tag "application" %>
7:   <%= csrf_meta_tags %>
8: </head>
9: <body>

抛出错误的页面文件 -

app/views/tickets/_form.html.erb

<%= form_for(@ticket) do |f| %>
<% if @ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2>
<ul>
<% @ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :school_id %><br />
<%= f.collection_select :school_id, School.order(:name), :id, :name, include_blank: true%>
</div>

<div class="field">
<%= f.label :location_id %><br />
<%= f.grouped_collection_select :location_id, School.order(:name), :locations, :name, :id, :name, include_blank: true%>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

app/assets/javascripts/application.js包含:

//= require jquery
//= require jquery_ujs
//= require_tree .

带有 jQ​​uery 的 Coffeescript 文件 -

应用程序/资产/javascripts/tickets.js.coffee

$ ->
$('#ticket_location_id').parent().hide()
  locations = $('#ticket_location_id').html()
$('#ticket_school_id').change ->
  school = $('#ticket_school_id :selected').text()
  options = $(locations).filter("optgroup[label='#{school}']").html()
if options
$('#ticket_location_id').html(options)
$('#ticket_location_id').parent().show()
else
$('#ticket_location_id').empty
$('#ticket_location_id').parent().hide()

POST_IF 错误已通过缩进if / else我的咖啡脚本中的语句得到解决!(有关详细信息,请参阅下面的答案)没有错误并且页面加载!

4

1 回答 1

5

您的if陈述缺少缩进。缩进在 CoffeeScript中至关重要。

这...

if options
$('#ticket_location_id').html(options)
$('#ticket_location_id').parent().show()
else
$('#ticket_location_id').empty
$('#ticket_location_id').parent().hide()

...需要这样,前导缩进不是可选的:

if options
  $('#ticket_location_id').html(options)
  $('#ticket_location_id').parent().show()
else
  $('#ticket_location_id').empty
  $('#ticket_location_id').parent().hide()
于 2013-04-05T15:42:38.100 回答