Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: unexpected POST_IF error Rails

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!

like image 693
Devin Avatar asked Apr 05 '13 04:04

Devin


1 Answers

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()
like image 80
meagar Avatar answered Oct 21 '22 00:10

meagar