5

I'm running jQuery in app/assets/javascripts/ticket.js.coffee for a specific view. Every time I visit the page the browser renders this error - there is no mention of this error anywhere online.

(localhost: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>

File of the page throwing the error -

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 contains:

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

Coffeescript file with jQuery -

app/assets/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()

The POST_IF error has been resolved by indenting the if / else statements in my coffee script! (see answer below for details) There are no errors and the page loads!

6
  • Are you sure that's all the coffeescript code you have? Replace //= require_tree . with //= require tickets and let me know if you still have the same issue. The error you have is a bad parsing of coffeescript, if that doesn't work, try to remove the file tickets.js.coffee to see if it works, so at least we can realize that the issue is within the file tickets.js.coffee Commented Apr 5, 2013 at 7:08
  • I second the removal of //= require_tree . It has caused me all types of pain in the past. Its better to sperate all your javascripts into folders and then require those folders then it is to use the //= require_tree. Commented Apr 5, 2013 at 7:38
  • replacing <b>//= require_tree .</b> with <b>//= require tickets</b> gives me the same error, no change. if I remove the jQuery in the tickets.js.coffee file my pages loads correctly. in fact, if I remove the if and else statement in the jQuery, the page loads but the jQuery still doesn't run. Commented Apr 5, 2013 at 15:12
  • how would I require those folders if i separate my javascript? would i add the code to each page or into my application.js file? Commented Apr 5, 2013 at 15:15
  • 1
    Please don't listen to the above advice; require_tree is fine, and you should keep using it. Your error is completely unrelated to your use of require_tree. Commented Apr 5, 2013 at 15:44

1 Answer 1

5

You're missing indentation for your if statement. Indentation is crucial in CoffeeScript.

This...

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

... needs to be this, the leading indentation is not optional:

if options
  $('#ticket_location_id').html(options)
  $('#ticket_location_id').parent().show()
else
  $('#ticket_location_id').empty
  $('#ticket_location_id').parent().hide()
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, thanks! I didn't find that anywhere! This is great, the POST_IF error doesn't show up anymore so I'm a step closer but now it's still not running my jQuery. Any ideas?
I got it to work! It must have been something with white space and indentation in my jQuery also that wasn't running it. I didn't know Coffeescript was so picky about whitespace. Thank you!
CoffeeScript is all about whitespace, the level of indenting before a line of code is extremely important. Your if statements don't do anything if their bodies aren't indented. How else do you expect CoffeeScript to know what lines go with which statement, when you don't have nothing denoting the close of a block like end or } or </tag>?
so basically with CoffeeScript you trade the problem of brackets for the problem of white-space and indentation. Never really understood how that is useful.
@MaxHodges This is hardly unique to CoffeeScript. There are many extremely popular languages where leading whitespace is meaningful. As for how it's useful, I can objectively say that it leads to shorter, more readable code, but your mileage may vary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.