2013-04-05 64 views
5

对于特定视图,我在app/assets/javascripts/ticket.js.coffee中运行jQuery。每次访问该页面时,浏览器都会呈现此错误 - 在线任何地方都没有提及此错误。SyntaxError:意外的POST_IF错误Rails

(本地主机:3000 /票/新)

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> 

引发错误页面的文件 -

应用程序/视图/票/ _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 c ontains:

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

与jQuery的CoffeeScript文件 -

应用程序/资产/ Java脚本/ 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语句解析在我的咖啡剧本中! (有关详细信息,请参阅下面的答案)没有错误和页面加载!

+0

你确定这是所有的CoffeeScript代码,你有吗?将** // = require_tree。**替换为** // =需要门票**,并让我知道您是否仍然有相同的问题。您的错误是对coffeescript的错误解析,如果不起作用,请尝试删除文件tickets.js.coffee以查看它是否有效,所以至少我们可以认识到问题在文件tickets.js中.coffee – rorra 2013-04-05 07:08:30

+0

我第二次移除// = require_tree。它在过去造成我所有类型的痛苦。它更好地将所有的JavaScript文件分配到文件夹中,然后需要这些文件夹,然后使用// = require_tree。 – rovermicrover 2013-04-05 07:38:54

+0

替换// = require_tree。// =要求门票给我同样的错误,没有变化。如果我删除了tickets.js.coffee文件中的jQuery,我的页面将正确加载。实际上,如果我在jQuery中删除了'if'和'else'语句,页面将加载,但jQuery仍然不会运行。 – Devin 2013-04-05 15:12:41

回答

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() 
+0

哇,谢谢!我没有在任何地方找到!这很好,POST_IF错误不再显示,所以我更近了一步,但现在它仍然没有运行我的jQuery。有任何想法吗? – Devin 2013-04-05 16:42:26

+0

我得到它的工作!它一定是在我的jQuery也没有运行它的空白和缩进的东西。我不知道Coffeescript对空白是如此挑剔。谢谢! – Devin 2013-04-05 17:51:17

+0

CoffeeScript是关于*空格的*全部,在一行代码之前的缩进级别非常重要。如果他们的身体没有缩进,你的if语句就不会做任何事情。当你没有任何东西表示像'end'或''''或''这样的块关闭时,你还希望CoffeeScript知道哪些行与哪条语句一致? – meagar 2013-04-05 17:58:04

相关问题